Interesting question. I think the code is fine. From the spec <https://golang.org/ref/spec#Go_statements>:
> The function value and parameters are evaluated as usual in the calling goroutine, but unlike with a regular call, program execution does not wait for the invoked function to complete. This means `onedoer.do` is immediately evaluated, before the loop continues. The usual issue with loops is due to closures. So, this would be wrong and have exactly the issue you are worried about: for _, onedoer := range d { go func() { oneoder(&wg) } } But the go statement isn't equivalent to that, it doesn't close over the function value, it evaluates it. On Thu, Mar 4, 2021 at 5:01 PM Julien Pivotto <roidelapl...@gmail.com> wrote: > Hello, > > In the following example: https://play.golang.org/p/yz_ifHC-Hut > > for _, onedoer := range d { > go onedoer.do(&wg) > } > > Should I pass the function onedoer.do as a parameter of the go routine: > https://play.golang.org/p/WHPahoayDbM ? > > for _, onedoer := range d { > go func(od *doer, w *sync.WaitGroup) { > od.do(w) > }(onedoer, &wg) > } > > I am wondering if `go func()` could return before figuring out which > function to run, creating a race with the loop. > > > Thanks in advance! > > -- > 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 golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/6547c92f-a009-4fd4-abb2-801b2c44ea67n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/6547c92f-a009-4fd4-abb2-801b2c44ea67n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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 golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHEXP_EamRKcxqCu0Ydgo1qkKfbn0K2KSewtTfu-V6djg%40mail.gmail.com.