https://github.com/golang/go/blob/dev.go2go/src/cmd/go2go/te...
A general question. Is there a reason to add those arguments instead of just using them directly inside the goroutine and so passing the arguments implicitly?
A general question. Is there a reason to add those arguments instead of just using them directly inside the goroutine and so passing the arguments implicitly?
In this case, no, and it's actually slightly inefficient to pass those variables as arguments, because a copy of the argument has to be made. This copying matters more if the argument is a big struct or another large value.
However often it is useful to use arguments, for example when calling a goroutine in a loop. For example:
for i := 0; i < 10; i++ {
go func() {
fmt.Println(i)
}()
}
vs for i := 0; i < 10; i++ {
go func(i int) {
fmt.Println(i)
}(i)
}
have different behaviours, because the first receives the (changing) `i` variable itself, whereas the second receives a copy of `i` at each iteration. The second behaviour is often the behaviour we actually want.Something like
for i := 0; i < 10; i++ {
i := i
go func() {
fmt.Println(i)
}()
}
also works as expected.