Your goroutines don't capture the current value of the variables i
and v
, but rather they reference the variables themselves. In this case, the 5 spawned goroutines did not get scheduled until the for loop finished, so all printed out the last values of i
and v
.
If you want to capture the current values of some variables for the gouroutine, you could modify the code to read something like the following:
go func(i, v int){
fmt.Println(i,v)
}(i, v)
Now each gouroutine has its own copy of the variables holding the value at the time it was spawned.