Go 语言也有 指针( pointer ),指针用于保存一个值的内存地址。
类型 *T 就是类型 T 的指针类型。 指针的 零值( zero value )是: nil 。
操作符( operator ) & 用来取 被操作数( operand )的指针(内存地址):
操作符 * 用来取出指针指向的值:
| 1
2
 | fmt.Println(*p)
*p = 21
 | 
 
这个操作简称 取值( dereferencing )。
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 | package main
import "fmt"
func main() {
    i, j := 42, 2701
    p := &i         // point to i
    fmt.Println(*p) // read i through the pointer
    *p = 21         // set i through the pointer
    fmt.Println(i)  // see the new value of i
    p = &j          // point to j
    *p = *p / 37    // divide j through the pointer
    fmt.Println(j)  // see the new value of j
}
 | 
 
与 C 语言不同,Go 语言指针 没有指针算术( pointer arithmetic )。
【小菜学Go语言】系列文章首发于公众号【小菜学编程】,敬请关注:
