I am trying to initialize an array of items in concurrently manner using go routines. However, the strange memory behavior of Go prevents me from doing so even I used the suggested primitives (channel). Below is the minimal reproduce:
func TestSliceInit(t *testing.T){
toInit := make([]int, 10)
syncLock := make(chan bool)
for i := range toInit{
go func(){toInit[i] = i; syncLock <- true}()
}
for range toInit{
<-syncLock
}
for i := range toInit{
if toInit[i] != i{
t.Error("fail to init")
}
}
}
The code is supposed to initialize the toInit
array to 0-9 but it does not. Instead, error will be produced. I tried this code on Goland 2018.1