Hello,
When spawning goroutines in a for loop - is there any reason to prefer
approach #1 below versus #2 or vice-versa?
Approach #1: Using a closure
func printme(val int) {
// Do something with val
}
func main() {
slice := []int{1, 23, 100, 101}
for _, val := range slice {
go func(val int) {
printme(val)
}(val)
}
}
Approach #2: Direct
func printme(val int) {
// Do something with val
}
func main() {
slice := []int{1, 23, 100, 101}
for _, val := range slice {
go printme(val)
}
}
I think both these approaches are fine, but just wanted to check.
Thanks for your inputs.
Best Wishes,
Amit.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.