Which fix removes the bug?
for _, p := range people {
p.Age++
}
fmt.Println(people) // ages unchanged
- Use
p.Age += 1instead - Range with
for p := range - Index it:
people[i].Age++ - Make Age an
int64field
range copies each struct into p, so the change is made to the copy; index into the slice to modify it.
Play Problem Solving ▸