Code with Go
Ask or search…
K
Links

Wait Groups

This Go code demonstrates the use of the sync.WaitGroup to wait for a collection of goroutines to finish their execution. Let's go through it with inline comments:
package main
import (
"fmt"
"sync"
"time"
)
// worker function represents a worker that performs some work
func worker(id int) {
fmt.Printf("Worker %d starting\n", id)
// Simulate work by sleeping for one second
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
}
func main() {
// Creating a WaitGroup to wait for all goroutines to finish
var wg sync.WaitGroup
for i := 1; i <= 5; i++ {
// Incrementing the WaitGroup counter for each goroutine
wg.Add(1)
// Creating a local variable 'i' to capture the current value
i := i
// Launching a goroutine for each worker
go func() {
// Decrementing the WaitGroup counter when the goroutine completes
defer wg.Done()
worker(i)
}()
}
// Waiting for all goroutines to finish
wg.Wait()
}

Output

Worker 2 starting
Worker 3 starting
Worker 5 starting
Worker 4 starting
Worker 1 starting
Worker 1 done
Worker 4 done
Worker 5 done
Worker 2 done
Worker 3 done
Explanation:
  1. 1.
    package main: Indicates that this Go file belongs to the main executable package.
  2. 2.
    import (...): Imports necessary packages, including "fmt" for formatting and printing, "sync" for synchronization, and "time" for handling time-related operations.
  3. 3.
    func worker(id int) { ... }: Defines a worker function that simulates work by sleeping for one second and prints messages.
  4. 4.
    var wg sync.WaitGroup: Creates a sync.WaitGroup variable named 'wg' to wait for a collection of goroutines to finish.
  5. 5.
    wg.Add(1): Increments the WaitGroup counter for each goroutine to indicate that a new goroutine is starting.
  6. 6.
    i := i: Creates a local variable 'i' to capture the current value of the loop variable, preventing its value from changing in the closure.
  7. 7.
    go func() { ... }(): Launches a goroutine for each worker. The defer wg.Done() statement is used to decrement the WaitGroup counter when the goroutine completes.
  8. 8.
    wg.Wait(): Waits for all goroutines to finish by blocking until the WaitGroup counter becomes zero.
This code demonstrates how to use a sync.WaitGroup to coordinate the execution of multiple goroutines. Each worker is launched as a goroutine, and the WaitGroup is used to wait for all workers to complete their work before proceeding further in the main function. The use of a local variable inside the loop ensures that each goroutine captures the correct value of 'i'.