Go 结构的内存地址不可见
在Go中,我不明白为什么像int这样的变量的内存地址可以获得,而结构却不能。例如:Go 结构的内存地址不可见,go,Go,在Go中,我不明白为什么像int这样的变量的内存地址可以获得,而结构却不能。例如: package main import "fmt" func main() { stud1 := stud{"name1", "school1"} a:=10 fmt.Println("&a is:", &a) fmt.Println("&stud1 is:",&stud1) } 输出为: &a is: 0x20818a220 &stud1 is: &
package main
import "fmt"
func main() {
stud1 := stud{"name1", "school1"}
a:=10
fmt.Println("&a is:", &a)
fmt.Println("&stud1 is:",&stud1)
}
输出为:
&a is: 0x20818a220
&stud1 is: &{name1 school1}
为什么&a给出了内存地址,而&stud1没有给出确切的内存位置。我没有任何使用内存地址的意图,只是对不同的行为感到好奇。包使用反射来打印值,并且有一个特定的情况,将指向结构的指针打印为&{Field Value}
如果要查看内存地址,请使用指针格式动词%p
fmt.Printf("&stud is: %p\n", &stud)