~/Overview of Golang sync Package
Feb 2, 2024
The sync package in Golang provides primitives for concurrency including mutual exclusion, wait groups, and once-only execution.
Mutex provides a lightweight lock for sharing data safely between goroutines.
Example of Mutex
WaitGroup allows waiting for a set of goroutines to finish.
Example of WaitGroup
Once executes a function only one time regardless of how many goroutines invoke it.
Example of Once
The sync package also includes RWMutex, Cond, and Pool for advanced use cases. For more, consult the Go sync documentation.