$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #227

Which fix removes the bug?

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