TIL about sync.WaitGroup in Golang
by @0xi4o
WaitGroup in the sync package is used to wait for a collection of goroutines to finish. It's a simple counter to keep track of the number of goroutines that are spawned. Right before I create a goroutine, I can call `WaitGroup.Add(1)` to increment the counter. To decrement, ie., when a goroutine has finished, I have to call `WaitGroup.Done()`. To wait for all goroutines to finish, I can use `WaitGroup.Wait()`.
WaitGroup is useful when setting up graceful shutdown where multiple goroutines might be doing tasks and we want to wait for all of them to be completed before stopping the server.
Source: Let's Go Further by Alex Edwards and sync package docs
WaitGroup is useful when setting up graceful shutdown where multiple goroutines might be doing tasks and we want to wait for all of them to be completed before stopping the server.
Source: Let's Go Further by Alex Edwards and sync package docs