可直接使用 & 操作符取地址的对象,就是可寻址的(Addressable)。比如下面这个例子
func main() {
name := "iswbm"
fmt.Println(&name)
// output: 0xc000010200
}
程序运行不会报错,说明 name 这个变量是可寻址的。
但不能说 "iswbm" 这个字符串是可寻址的。
"iswbm" 是字符串,字符串都是不可变的,是不可寻址的,后面会介绍到。
在开始逐个介绍之前,先说一下结论
指针可以寻址:&Profile{}
变量可以寻址:name := Profile{}
字面量通通不能寻址:Profile{}
# 2. 哪些是可以寻址的?
变量:&x
func main() {
name := "iswbm"
fmt.Println(&name)
// output: 0xc000010200
}
指针:&*x
type Profile struct {
Name string
}
func main() {
fmt.Println(unsafe.Pointer(&Profile{Name: "iswbm"}))
// output: 0xc000108040
}