This code:
import "fmt"
import "time"
func main() {
string_slice:=[]string{"a","b","c"}
for _,s:=range string_slice{
go func(){
time.Sleep(1*time.Second)
fmt.Println(s)
}()
}
time.Sleep(3*time.Second)
}
produces the output "c c c", while this code:
import "fmt"
func main() {
string_slice:=[]string{"a","b","c"}
for _,s:=range string_slice{
s="asd"
fmt.Println(s)
}
fmt.Println(string_slice)
}
produces the output "[a b c]"
The first one would suggest that for range iterates over references (which it should not), and the second one suggests that it iterates over copies of values (which it should).
Why doesn't the first one produce the output "a b c"?
When uses goroutine you must assume that it will running in parallelo. So in this case might occur 'c c c' as too 'b b b' or 'a a a'
On run in 3 times this code:
You will send for running 3 times this code:
So, might occur of func() start running in t2, per example. In this case the result will 'c c c' because s equals latest value (string_slice[2]).
The solution is copy value by func params:
Or create new value per iteration
See working in https://play.golang.org/p/uRD6Qy6xSw