我无法理解此代码块的行为。我做错了什么,正确的方法应该是什么?
import (
"fmt"
"strconv"
)
type Record struct {
name *string
}
type person struct {
name string
}
func main() {
var Records []*Record
var persons []person
for i := 0; i < 10; i++ {
newValue := person{name: strconv.Itoa(i)}
persons = append(persons, newValue)
}
for _, personone := range persons {
newRecord := &Record{}
getName(newRecord, &personone)
Records = append(Records, newRecord)
}
for _, record := range Records {
fmt.Println(*record.name)
}
}
func getName(record *Record, value *person) {
record.name = &value.name
}
我期望此代码打印 0 到 9,但它始终打印最后一个值 9。
MMTTMM
相关分类