← All notes

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+

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

Before WaitGroup.Go

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

Rules

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:ahs4b0gx

iinventedlight. 2026. "sync.WaitGroup waits for a group of goroutines; WaitGroup.Go starts and tracks each one." iinventedlight Library, iil:note:ahs4b0gx, version of 2026-11-18. https://iinventedlight.com/library/notes/go-waitgroup/. License: CC-BY-4.0.

@misc{iil_note_ahs4b0gx,
  author = {{iinventedlight}},
  title = {{sync.WaitGroup waits for a group of goroutines; WaitGroup.Go starts and tracks each one}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/go-waitgroup/},
  note = {Permanent ID iil:note:ahs4b0gx. 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-waitgroup.md.