Table of Contents

sync.WaitGroup

What is it?

`sync.WaitGroup` is a counter-based synchronization primitive used to wait for a set of goroutines to finish.

What is it used for?

When to use

Example

package main
 
import (
    "fmt"
    "sync"
)
 
func main() {
    var wg sync.WaitGroup
 
    for i := 0; i < 3; i++ {
        wg.Add(1)
        go func(id int) {
            defer wg.Done()
            fmt.Println("worker:", id)
        }(i)
    }
 
    wg.Wait()
    fmt.Println("all done")
}

Notes / pitfalls

Hard words (English)