← Back to context

Comment by p2detar

4 days ago

I'm still relatively new to Go, but I've never seen closures used that often thus far in production code. Is it really a common practice?

That first example is an unintended closure, since the err at the top level actually has nothing to do with the errs in the goroutines. I have seen that sometimes, although the use of = rather than := normally makes it obvious that something dodgy is going on.

As to whether it's a common pattern, I see closures on WaitGroups or ErrGroups quite often:

  workerCount := 5
  var wg sync.WaitGroup
  wg.Add(workerCount)

  for range workerCount {
    go func() {
      // Do work
      wg.Done()
    }()
  }

  wg.Wait()

You can avoid the closure by making the worker func take a *sync.WaitGroup and passing in &wg, but it doesn't really have any benefit over just using the closure for convenience.