sync
Concurrencyimport "sync"Provides synchronization primitives like mutexes and wait groups for goroutine coordination.
Example
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
fmt.Println(n)
}(i)
}
wg.Wait()
}Key Types & Functions
MutexRWMutexWaitGroupOnceMapPoolAbout sync
The sync package (imported as sync) belongs to the Concurrency category of Go packages. Provides synchronization primitives like mutexes and wait groups for goroutine coordination.
Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The sync package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.
When using sync in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.