type student struct {
Name string
Age int
}
func main() {
m := make(map[string]*student)
s := []student{
{Name: "Allen", Age: 24},
{Name: "Tom", Age: 23},
}
for _, stu := range s {
m[stu.Name] = &stu
}
fmt.Println(m)
for key, value := range m {
fmt.Println(key, value)
}
}
结果:
map [Allen:0xc42006a0c0 Tom:0xc42006a0c0]
Allen& {Tom 23}
汤姆& {汤姆23}
如何解释切片现象,在我看来,stu应该是s的每个成员的地址,但从结果来看,s具有相同的地址。