0Pricing
Go Academy · Lesson

errgroup for Concurrent Error Handling

golang.org/x/sync/errgroup in practice

Problem with goroutines and errors

Launching goroutines with go func() discards return values including errors. You need extra machinery (channels, WaitGroup) to collect them. errgroup simplifies this.

golang.org/x/sync/errgroup

errgroup.Group runs goroutines and collects errors. Wait() blocks until all goroutines finish and returns the first non-nil error.

var g errgroup.Group
g.Go(func() error { return fetchUsers() })
g.Go(func() error { return fetchOrders() })
if err := g.Wait(); err != nil {
    log.Fatal(err)
}

All lessons in this course

  1. Pipeline and Stage Patterns
  2. errgroup for Concurrent Error Handling
  3. Semaphore Pattern
  4. Leak Detection and Cancellation
← Back to Go Academy