In what scenarios is a sync.WaitGroup more appropriate than using a channel to wait for goroutine completion?
Last updated
Use
sync.WaitGroupwhen you only need to wait for N goroutines to finish and need no return values. It is simpler and cheaper than counting receives on a done-channel.Use channels when goroutines produce results you must collect, aggregate, or stream back.
They combine well: workers send results on a channel while a
WaitGroupdecides when toclose()it.