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"?