---
source: https://iinventedlight.com/library/notes/go-waitgroup.md
title: "sync.WaitGroup waits for a group of goroutines; WaitGroup.Go starts and tracks each one"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go standard library docs (pkg.go.dev)); CC-BY-4.0 (The Go Blog); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Go standard library docs (pkg.go.dev), sync package; The Go Blog, Go Concurrency Patterns: Pipelines and cancellation. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). 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 sync.WaitGroup"
tags: [go, sync, WaitGroup, goroutines, concurrency]
client: Librarian
sources:
  - id: S1
    title: "sync package"
    publisher: Go standard library docs (pkg.go.dev)
    url: https://pkg.go.dev/sync
    accessed: 2026-11-18
    license: "BSD-3-Clause"
  - id: S2
    title: "Go Concurrency Patterns: Pipelines and cancellation"
    publisher: The Go Blog
    url: https://go.dev/blog/pipelines
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# sync.WaitGroup waits for a group of goroutines; WaitGroup.Go starts and tracks each one
**Answer:** A WaitGroup is a counting semaphore, typically used to wait until a group of goroutines or tasks finish. A main goroutine typically calls `WaitGroup.Go` to start each task in a new goroutine, then calls `WaitGroup.Wait` to wait for all tasks to complete. The `Add` and `Done` pattern is common in code that predates `WaitGroup.Go`.

## Go 1.25+

```go
var wg sync.WaitGroup
for _, url := range urls {
    wg.Go(func() { http.Get(url) })
}
wg.Wait()
```

## Before WaitGroup.Go

```go
var wg sync.WaitGroup
wg.Add(1)
go func() {
    defer wg.Done()
    task1()
}()
wg.Wait()
```

## Rules

- `Go` calls `f` in a new goroutine and adds that task to the WaitGroup.
- `Add` adds delta (which may be negative) to the task counter of the WaitGroup; when the counter becomes zero, goroutines blocked on `Wait` are released.
- A WaitGroup must not be copied after first use.
- A call to `Done` "synchronizes before" the return of any `Wait` call that it unblocks.

## Closing channels safely

In a fan-in pipeline, sends on a closed channel panic, so all sends must be done before calling `close`. The `sync.WaitGroup` type provides a simple way to arrange this: start an output goroutine for each input channel, then close the output channel after `Wait` returns.

## See also

- [go-goroutines-channels](https://iinventedlight.com/library/notes/go-goroutines-channels/)
- [go-context](https://iinventedlight.com/library/notes/go-context/)
- [go-race-detector](https://iinventedlight.com/library/notes/go-race-detector/)

## Claims

- id: C1
  claim: A WaitGroup waits for a group of goroutines or tasks to finish.
  source: S1
  evidence: "A WaitGroup is a counting semaphore typically used to wait for a group of goroutines"

- id: C2
  claim: WaitGroup.Go was added in go1.25.0.
  source: S1
  evidence: "added in go1.25.0 func (wg * WaitGroup ) Go(f func())"

- id: C3
  claim: A WaitGroup must not be copied after first use.
  source: S1
  evidence: "A WaitGroup must not be copied after first use."

- id: C4
  claim: Sends on a closed channel panic.
  source: S2
  evidence: "Sends on a closed channel panic"

## Sources

- [S1] sync package. Go standard library docs (pkg.go.dev). https://pkg.go.dev/sync . License: BSD-3-Clause. Accessed 2026-11-18.

- [S2] Go Concurrency Patterns: Pipelines and cancellation. The Go Blog. https://go.dev/blog/pipelines . License: CC-BY-4.0. 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: Go standard library docs (pkg.go.dev), sync package; The Go Blog, Go Concurrency Patterns: Pipelines and cancellation. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). 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).
