I think this explains it pretty well: Common Mistakes: Using goroutines on 
loop iterator variables 
<https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables>

On Thursday, February 17, 2022 at 11:45:39 AM UTC-5 yan.z...@gmail.com 
wrote:

> package main
> import "fmt"
>
> func main() {
>     targetIndice := make([]int, 3)
>     targetIndice[0] = 5
>     targetIndice[2] = 4
>     
>     for i,n := range targetIndice{
>         fmt.Printf("%d : %d \n", i, n)
>     }
>     
>     c := make(chan int)
>     for i, n:= range targetIndice{
>         go func(){
>             fmt.Printf("targetIndice[%d]=%d \n", i, n)
>             c <- 1
>         }()
>     }
>     
>     for i:=0; i<3; i++{
>         <- c
>     }
>     
> }
>
> -----go run main.go---------
> 0 : 5 
> 1 : 0 
> 2 : 4 
> targetIndice[2]=4 
> targetIndice[2]=4 
> targetIndice[2]=4 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4e22e6ae-2114-44d1-bca8-f649b0f84d8cn%40googlegroups.com.

Reply via email to