type Tmp struct { Name string Age int } func main( data := []Tmp{ {"zhangsan", 18}, {"lisi", 22}, {"wangwu", 15}, } for _, v := range data { v.Age = 999 } fmt.Println(data) for i := 0; i < len(data); i++ { data[i].Age = 999 } fmt.Println(data) )
range data 对于切片操作是值操作,并不是指针操作,所以业务中最好使用len(data)索引方式对值进行操作
result: [{zhangsan 18} {lisi 22} {wangwu 15}] [{zhangsan 999} {lisi 999} {wangwu 999}]