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
- Pipeline and Stage Patterns
- errgroup for Concurrent Error Handling
- Semaphore Pattern
- Leak Detection and Cancellation