Junior — Middle
Did you use the synchronization mechanism waitgroup in your implementation of your program?
sobes.tech AI
Answer from AI
Yes, in Go, the sync.WaitGroup mechanism is often used for synchronizing multiple goroutines. It allows the main goroutine to wait for a set of parallel tasks to complete.
Example of usage:
var wg sync.WaitGroup
wg.Add(3) // expecting 3 goroutines
for i := 0; i < 3; i++ {
go func(id int) {
defer wg.Done() // signal completion
fmt.Printf("Goroutine %d is running\n", id)
// some work
}(i)
}
wg.Wait() // wait until all Done are called
fmt.Println("All goroutines are finished")
Thus, WaitGroup helps to manage concurrency correctly and ensures that the program does not terminate prematurely.