WaitGroup
WaitGroup is a synchronization type that counts concurrent operations and allows execution to wait until the counter reaches zero.
Learn Concurrent Programming with Go
A waitgroup is a synchronization object that allows execution to wait until a counted set of concurrent operations has completed.
- How it is used
- Coordinates a main or waiting goroutine with a group of concurrent tasks that must finish before execution proceeds.
- How deep it goes
- Explains Add, Done, and Wait, then builds fixed and dynamically sized implementations from semaphores and condition variables.
- Examples
- Uses four delayed workers, concurrent letter-frequency counting, and recursive filename search.
- What you can do after
- Build it from scratch with semaphore or condition-variable primitives, and apply sync.WaitGroup to await concurrent work.
Read how Learn Concurrent Programming with Go explains WaitGroup →
Learn Go with Pocket-Sized Projects
sync.WaitGroup is a Go synchronization type that allows one goroutine to wait for a collection of goroutines to finish.
- How it is used
- Coordinates goroutines so a controlling goroutine does not finish before registered concurrent work completes.
- How deep it goes
- Explains Add, Done, and Wait with runnable examples, pointer semantics, loop registration, and coordinator placement.
- Examples
- Uses cooking tasks, a ten-writer cache test, and a concurrent maze solver listener.
- What you can do after
- Build it from scratch to coordinate goroutines, wait for completion, and structure concurrent tests.
Read how Learn Go with Pocket-Sized Projects explains WaitGroup →
Go in Action, Second Edition
WaitGroup is a Go synchronization type that counts active operations and blocks until its counter reaches zero.
- How it is used
- Coordinates goroutine completion before a caller proceeds or a results channel closes.
- How deep it goes
- Explains counter-based waiting and demonstrates both Go and Add/Done patterns, while leaving error collection to other mechanisms.
- Examples
- Demonstrates concurrent feed fetching and an HTTP handler that aggregates articles and errors.
- What you can do after
- Apply it via Go's sync package to wait for concurrent operations and coordinate results-channel closure.
Go in Practice, Second Edition
A wait group is a synchronization primitive that lets a program wait for a collection of concurrent tasks to finish.
- How it is used
- Coordinates goroutines performing independent jobs before the coordinating process safely continues.
- How deep it goes
- Defines count-based synchronization and demonstrates Add, Done, and Wait in a parallel file-compression program.
- Examples
- Uses command-line file compression with compress/gzip and produces .gz files before printing a summary.
- What you can do after
- Build it from scratch by coordinating multiple goroutines and waiting for their completion.
Read how Go in Practice, Second Edition explains WaitGroup →
100 Go Mistakes and How to Avoid Them
sync.WaitGroup is a Go synchronization type that allows code to wait for a specified number of concurrent operations to complete.
- How it is used
- Coordinates completion of a known group of goroutines before subsequent code proceeds.
- How deep it goes
- Explains the zero value, counter operations, blocking Wait, panic condition, and Add-ordering failure, but not implementation internals.
- Examples
- Demonstrates three goroutines updating an atomic counter, premature Wait completion, and corrected Add-before-go patterns.
- What you can do after
- Diagnose/debug with it by placing Add before goroutine creation and checking race-detector reports for premature completion.
Read how 100 Go Mistakes and How to Avoid Them explains WaitGroup →
Also covered in
What to know first
- goroutineA goroutine is a lightweight function execution unit managed by the Go runtime that runs concurrently with other program activity.
- channelA channel is a medium or logical pathway for conveying information, messages, or other content between sources and recipients.
- concurrencyConcurrency is the structuring or execution of multiple computational activities so their progress overlaps in time, whether or not they run simultaneously.
- handlerA handler is a software routine or component invoked to process an event, request, message, or other input.
Related concepts
- WaitGroup.Waithas part · WaitGroup.Wait is a method that blocks until the associated wait group's counter reaches zero.
- WaitGroup Done methodhas part · WaitGroup.Done is a method that decrements a wait group's counter by one to mark a tracked operation as finished.
- goroutineuses · A goroutine is a lightweight function execution unit managed by the Go runtime that runs concurrently with other program activity.
- sync packagepart of · The sync package is Go’s standard-library package providing synchronization primitives for coordinating concurrent execution and protecting shared data.
- add methodhas part · The Add method of sync.WaitGroup changes the number of concurrent operations that a WaitGroup waits to complete.
- barriercontrasts with · A barrier is a synchronization point that makes participating concurrent threads or processes wait until the required participants arrive before proceeding.
Questions about WaitGroup
What is WaitGroup?
WaitGroup is a synchronization type that counts concurrent operations and allows execution to wait until the counter reaches zero.
Which Manning books explain WaitGroup?
Learn Concurrent Programming with Go, Learn Go with Pocket-Sized Projects, Go in Action, Second Edition, Go in Practice, Second Edition and 100 Go Mistakes and How to Avoid Them.
What should I know before learning WaitGroup?
Manning books that teach WaitGroup usually introduce goroutine, channel, concurrency and handler first.
What is the difference between WaitGroup and barrier?
WaitGroup is a synchronization type that counts concurrent operations and allows execution to wait until the counter reaches zero. A barrier is a synchronization point that makes participating concurrent threads or processes wait until the required participants arrive before proceeding.
What is WaitGroup part of?
WaitGroup is part of sync package. The sync package is Go’s standard-library package providing synchronization primitives for coordinating concurrent execution and protecting shared data.
What are the parts of WaitGroup?
WaitGroup includes WaitGroup.Wait, WaitGroup Done method and add method.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.




