type test struct {
a int8
b int8
c int8
d int8
}
n := test{
1, 2, 3, 4,
}
fmt.Printf("n.a %p\n", &n.a)
fmt.Printf("n.b %p\n", &n.b)
fmt.Printf("n.c %p\n", &n.c)
fmt.Printf("n.d %p\n", &n.d)
输出
n.a 0xc00000a0b8
n.b 0xc00000a0b9
n.c 0xc00000a0ba
n.d 0xc00000a0bb
结构体内变量使用不同地址片段
type student struct {
name string
age int
}
func main() {
m := make(map[string]*student)
stus := []student{
{name: "pprof.cn", age: 18},
{name: "测试", age: 23},
{name: "博客", age: 28},
}
for _, stu := range stus {
m[stu.name] = &stu
}
for k, v := range m {
fmt.Println(k, "=>", v.name)
}
}
输出
pprof.cn => 博客
测试 => 博客
博客 => 博客
实例化结构体数组,每个结构体指向同一块地址