Middle — Senior
Milline on Wg.Wait meetodi roll niitide sünkronses?
sobes.tech AI
Vastus AI-lt
The Wg.Wait() method from the sync package in Go is used to synchronize multiple goroutines. It blocks the execution of the calling goroutine until the WaitGroup counter reaches zero, meaning all goroutines added via Add() have called Done().
Example:
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
// work of the first goroutine
}()
go func() {
defer wg.Done()
// work of the second goroutine
}()
wg.Wait() // Wait for both goroutines to finish
Thus, Wg.Wait() guarantees that the program will not continue until all parallel tasks are completed.