Quiz · Goroutines and channels · lesson 3 of 13Quiz — WaitGroupWhat is sync.WaitGroup and what is its Add/Done/Wait contract?[A]sync.WaitGroup waits for a set of goroutines to finish. It holds a counter that Add(n) raises before the go statement and Done() lowers inside each goroutine, while Wait() blocks until it reaches zero. The group can be passed to each goroutine by value: every copy shares the counter of the original, so Done on a copy lowers the counter that Wait is watching, just as it would through a pointer.[B]sync.WaitGroup waits for a set of goroutines to finish. It holds a counter: call Add(n) before starting the goroutines to raise it, call Done() (usually via defer) inside each goroutine to drop it by one, and call Wait() to block until the counter reaches zero. The key rule is to Add in the launching goroutine before the go statement, not inside the new goroutine, or Wait may return too early.[C]sync.WaitGroup waits for a set of goroutines to finish. It holds a counter: call Add(n) to raise it, Done() (usually via defer) to drop it by one, and Wait() to block until it reaches zero. The Add call may go inside each new goroutine, as its first line: Wait checks the counter again whenever it changes, so it cannot return before that Add has run, even when the goroutine starts late.[D]sync.WaitGroup waits for a set of goroutines to finish. It holds a counter: call Add(n) before the go statement, call Done() inside each goroutine, and call Wait() to block until the counter reaches zero. Extra Done calls are harmless: once the counter is at zero, a further Done leaves it there, so calling Done more times than Add does not change the program. A Done without any matching Add is skipped in the same quiet way.Subscribe to take the courseThe course is free. Sign in with Telegram and subscribe to the channel to open every lesson.Continue with TelegramSign in another way← Backsync.WaitGroupNext →Channels