Go Tutorial

sync Package

WaitGroup, Mutex, Once, and Pool

Why the sync Package?

While channels are the preferred way to communicate in Go, sometimes you need more traditional low-level synchronization tools. The sync package provides these primitives.

1. WaitGroup

A WaitGroup is used to wait for a collection of goroutines to finish executing.
go
var wg sync.WaitGroup

for i := 1; i <= 5; i++ {
    wg.Add(1) // Increment the counter
    go func(id int) {
        defer wg.Done() // Decrement the counter when finished
        fmt.Printf("Worker %d starting\n", id)
    }(i)
}

wg.Wait() // Block until the counter is zero!

2. Mutex (Mutual Exclusion)

If you have a piece of data that multiple goroutines need to modify (like a counter), you use a Mutex to ensure only one goroutine can access it at a time.
go
var mu sync.Mutex
var count int

func increment() {
    mu.Lock()
    defer mu.Unlock() // Always unlock using defer!
    count++
}
Wait! If you only have many readers and one writer, use RWMutex. It allows multiple goroutines to read at the same time, but only one to write.

3. sync.Once

sync.Once ensures that a function is executed exactly once, no matter how many times it's called or how many goroutines are calling it. This is perfect for initializing singletons or database connections.
go
var once sync.Once
once.Do(func() {
    // Initialization code runs only once!
})

4. sync.Map

A standard Go map is not safe for concurrent use. sync.Map is a specialized map type designed for cases where keys are stable and there are many readers.

Summary

TermFull NameDescription
WaitGroupsync.WaitGroupWaiting for multiple tasks to finish
Mutexsync.MutexExclusive access to data (One writer)
RWMutexsync.RWMutexShared read access, exclusive write access
Oncesync.OnceRunning a task exactly once
Poolsync.PoolReusing allocated objects to reduce GC pressure
LockerInterfaceAnything that can Lock() and Unlock()
Best ForLow-level state, high-performance counters, caches
SafetyPrevents Data Races at runtime (Use go test -race!)
DeadlockErrorWhen goroutines wait for each other's locks forever
CopyingWARNINGDo NOT copy a Mutex or WaitGroup after use!
StandardPart of the Go core library since day one
LogicUse channels for communication; Use sync for state