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
Gocallsfin a new goroutine and adds that task to the WaitGroup.Addadds delta (which may be negative) to the task counter of the WaitGroup; when the counter becomes zero, goroutines blocked onWaitare released.- A WaitGroup must not be copied after first use.
- A call to
Done"synchronizes before" the return of anyWaitcall 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
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.
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
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.},
}
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.