liveBook · Manning

Errgroup.Group

As explained in 100 Go Mistakes and How to Avoid Them

errgroup.Group is a type that coordinates a group of goroutines and records the first non-nil error returned by them.

A common mistake is launching goroutines with sync.WaitGroup but leaving error handling as an unresolved comment. Shared error slices, mutexes, or error channels add coordination complexity. errgroup.Group combines synchronization, error propagation, and context cancellation: call Go with a func() error, return failures from that function, and call Wait to obtain the first non-nil error. section 9.13

Use WithContext when parallel calls should share cancellation: an early failure can make waiting for slower calls unnecessary, provided those calls respond to the context. section 9.13

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Defines WithContext, Go, and Wait and demonstrates an indexed results handler, without detailing package internals.
Examples
Uses parallel foo calls over circles, an indexed results slice, and a corrected errgroup handler.
What it assumes
Assumes familiarity with goroutines, contexts, synchronization, and returning errors from functions.
What you can do after
Build it from scratch using WithContext, Go, and Wait to collect results and return the first error.
How it connects
Connects goroutines, contexts, synchronization, error propagation, mutexes, and error channels.

Also explained in

Compare how 2 books explain errgroup.Group →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.