指针
指针地址和指针类型
每个变量在运行时都拥有一个地址,这个地址代表变量在内存中的位置。Go语言中使用&
字符放在变量前面对变量进行“取地址”操作。 Go语言中的值类型(int、float、bool、string、array、struct)都有对应的指针类型,如:*int
、*int64
、*string
等。取变量指针的语法如下:
a := &变量
// 举例
a := 1
b := &a
fmt.Printf("a:%d ptr:%p\n", a, &a); // a:1 ptr:0xc000098000
fmt.Println(b) // 0xc000098000
fmt.Printf("%T\n", b) // *int