File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -486,6 +486,47 @@ func main() {
486486
487487- ` p ` :带 0x 前缀的十六进制地址值。
488488- ` #p ` :不带 0x 前缀的十六进制地址值。
489+ - Go 没有指针运算。
490+
491+ Go 具有指针。 指针保存了变量的内存地址。
492+
493+ 类型 * T 是指向类型 T 的值的指针。其零值是 nil 。
494+
495+ ``` go
496+ var p *int
497+ ```
498+
499+ & 符号会生成一个指向其作用对象的指针。
500+
501+ ``` go
502+ i := 42
503+ p = &i
504+ ```
505+
506+ * 符号表示指针指向的底层的值。
507+
508+ ``` go
509+ fmt.Println (*p) // 通过指针 p 读取 i
510+ *p = 21 // 通过指针 p 设置 i
511+ ```
512+
513+ 这也就是通常所说的“间接引用”或“非直接引用”。
514+
515+ ``` go
516+ package main
517+ import " fmt"
518+ func main () {
519+ i , j := 42 , 2701
520+ p := &i // 指向我 i
521+ fmt.Println (*p) // 通过指针读 i
522+ *p = 21 // 通过指针设置 i
523+ fmt.Println (i) // 看到i的新值
524+
525+ p = &j // 指向我 j
526+ *p = *p / 37 // 通过指针划分 j
527+ fmt.Println (j) // 看到j的新值
528+ }
529+ ```
489530
490531</details >
491532
You can’t perform that action at this time.
0 commit comments