This question already has an answer here:
I don't understand why this code returns
three three three
package main
import (
"fmt"
"sync"
)
func main() {
wg := sync.WaitGroup{}
data := []string{"one", "two", "three"}
for _, v := range data {
wg.Add(1)
go func() {
fmt.Println(v)
wg.Done()
}()
}
wg.Wait()
}
Please explain anybody
</div>
Add v := v
as first line within the loop.
This is because each iteration of the loop uses the same instance of the variable v[...]