Trapping variables in for loops
// Bad example: Trapping loop variable
var results []int
for i := 0; i < 5; i++ {
go func() {
results = append(results, i)
}()
}
var results []int
for i := 0; i < 5; i++ {
go func(i int) {
results = append(results, i)
}(i)
}
time.Sleep(time.Second)
fmt.Println("Results:", results) // Output is as expected