---
source: https://iinventedlight.com/library/notes/go-errgroup.md
title: "Go errgroup.Group - wait on goroutines and cancel on the first error"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (pkg.go.dev); BSD-3-Clause (golang/sync (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go errgroup"
tags: [go, errgroup, goroutines, context, sync]
client: Librarian
sources:
  - id: S1
    title: "errgroup package"
    publisher: pkg.go.dev
    url: https://pkg.go.dev/golang.org/x/sync/errgroup
    accessed: 2026-11-18
    license: "BSD-3-Clause"
  - id: S2
    title: "errgroup.go"
    publisher: golang/sync (GitHub)
    url: https://github.com/golang/sync/blob/master/errgroup/errgroup.go
    accessed: 2026-11-18
    license: "BSD-3-Clause"
---
# 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

```go
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)` starts `f` in a new goroutine.

## See also

- [go-context](https://iinventedlight.com/library/notes/go-context/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
