A lot of times while developing Go applications we have to deal with slower tasks which we prefer to run in parallel by spinning up new goroutines. This adds some complexity when you have to handle errors of these goroutines. Lately I’ve learned about go-multierror by HashiCorp, which gives a very easy way to handle this.

Let’s see an example:

var g multierror.Group

g.Go(func() error {
  // your code
})

g.Go(func() error {
  // your code
})

g.Go(func() error {
  // your code
})

err := g.Wait().ErrorOrNil()
if err != nil {
  // handle error
}