package main import "fmt" type student struct { name string age int } func main() { students := []student{{"wjc",33},{"kelvin",11}} stuMap := make(map[int]*student) for i,stu := range students{ stuMap[i] = &stu fmt.Printf("%p,%+v ",&stu,stu) } for k,v := range stuMap{ fmt.Println(k,v) } for i,_:= range students{ stuMap[i] = &students[i] fmt.Printf("%p,%+v ",&students[i],students[i]) } for k,v := range stuMap{ fmt.Println(k,v) } }
//输出
0xc00005a440,{name:wjc age:33}
0xc00005a440,{name:kelvin age:11}
0 &{kelvin 11}
1 &{kelvin 11}
0xc00006a330,{name:wjc age:33}
0xc00006a348,{name:kelvin age:11}
0 &{wjc 33}
1 &{kelvin 11}
坑总结:
循环过程中,stu变量只声明了一次,所以stu地址即&stu是不变的,值是变化的。所以&stu始终不变