Go errgroup.Group - wait on goroutines and cancel on the first error
Answer: Wait blocks until all function calls from the Go method have returned. It then returns the first non-nil error. WithContext returns a new Group and an associated Context derived from the parent, canceled when the first function fails. SetLimit limits the number of active goroutines in this group.
Examples
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(8)
for _, url := range urls {
url := url
g.Go(func() error {
return fetch(ctx, url)
})
}
if err := g.Wait(); err != nil {
return err
}
Rules
Go(f)startsfin a new goroutine.
See also
Claims
- id: C1 claim: Wait blocks until all Go calls have returned. source: S1 evidence: "Wait blocks until all function calls from the Go method have returned"
- id: C2 claim: WithContext returns a Group and a derived Context. source: S2 evidence: "WithContext returns a new Group and an associated Context derived"
- id: C3 claim: SetLimit caps active goroutines in the group. source: S2 evidence: "SetLimit limits the number of active goroutines in this group"
Sources
- [S1] errgroup package. pkg.go.dev. https://pkg.go.dev/golang.org/x/sync/errgroup . License: BSD-3-Clause. Accessed 2026-11-18.
- [S2] errgroup.go. golang/sync (GitHub). https://github.com/golang/sync/blob/master/errgroup/errgroup.go . License: BSD-3-Clause. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: pkg.go.dev, errgroup package; golang/sync (GitHub), errgroup.go. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: pkg.go.dev, errgroup package; golang/sync (GitHub), errgroup.go. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go errgroup.Group - wait on goroutines and cancel on the first error." iinventedlight Library, iil:note:g0ageczs, version of 2026-11-18. https://iinventedlight.com/library/notes/go-errgroup/. License: CC-BY-4.0.
@misc{iil_note_g0ageczs,
author = {{iinventedlight}},
title = {{Go errgroup.Group - wait on goroutines and cancel on the first error}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/go-errgroup/},
note = {Permanent ID iil:note:g0ageczs. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-errgroup.md.