在Go中,我很困惑为什么像int这样的变量可以得到内存地址,而structs却不能。例如:
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 却没有给出确切的内存位置。我无意使用内存地址,只是对不同的行为感到好奇。
最佳答案
fmt
包使用反射来打印出值,并且有一个特定的案例将指向结构的指针打印为 &{Field Value}
。
如果要查看内存地址,使用指针格式化动词%p
。
fmt.Printf("&stud is: %p\n", &stud)
关于go - 结构的内存地址不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498374/