← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:g0ageczs

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.},
}

Markdown

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.