我的问题很简单,但我相信它隐藏了 Go 变量初始化的重要特征。
如果我们有两个变量
i := 5
d := &i
Run Code Online (Sandbox Code Playgroud)
我们想打印它们
fmt.Printf("The address of i value is %d\n", &i)
fmt.Printf("The value of d is %d\n", d)
fmt.Printf("The address of d is %d", &d)
Run Code Online (Sandbox Code Playgroud)
输出将类似于
The address of i value is 824633835664
The value of d is 824633835664
The address of d is 824633778224
Run Code Online (Sandbox Code Playgroud)
所以它看起来像&i返回其值的地址5。明白了。我们不明白的是那&d还什么?变量的地址i?
那么是不是这样实现的,值有自己的地址,变量(是值内存地址的别名)在内存中也有自己的地址呢?否则&d将返回我们的地址5