<分区>
使用此代码:https://play.golang.org/p/tCm1W-K-6ob
此代码将打印:[c c c]
,但 [a b c]
除外。
type A struct {
a *string
}
func f() {
slist := []string{"a", "b", "c"}
list := make([]*A, len(slist))
for i, v := range slist {
item := &A{
a: &v,
}
list[i] = item
}
fmt.Printf("[%s %s %s]", *list[0].a, *list[1].a, *list[2].a)
}
func main() {
f()
}
为什么列表不是 ["a", "b", "c"]
?范围或 &string
发生了什么?