import (
"fmt"
)
func Print(i int) {
fmt.Println(i)
}
func PrintR(i *int) {
fmt.Println(*i)
}
func main() {
for i := 0; i < 10; i++ {
go Print(i) // Get: 1 9 2 3 4 5 7 6 8 0
//go func() {
// Print(i)
//}() // Get: 10 10 7 10 4 10 7 10 10 10 10
//go PrintR(&i) // Get: 7 10 10 7 10 7 10 10 10 10
}
select {
}
}
The 2nd, 3rd example got the expected output, but I have thought that the 1st case's output go Print(i)
should be similar to the 2nd's since both of them do not use a new variable to freeze the current value of i. What cause the difference?