Values containing the types defined in this package should not be copied after first use.
Core Types
Mutex
A Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.Locks the mutex. If the lock is already in use, the calling goroutine blocks until the mutex is available.
Unlocks the mutex. It is a run-time error if the mutex is not locked on entry to Unlock.
Tries to lock the mutex and reports whether it succeeded. Note that while correct uses of TryLock exist, they are rare.
A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.
RWMutex
A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer.Locks rw for reading. Should not be used for recursive read locking.
Undoes a single RLock call.
Locks rw for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available.
Unlocks rw for writing.
Tries to lock rw for reading and reports whether it succeeded.
Tries to lock rw for writing and reports whether it succeeded.
Returns a Locker interface that implements Lock and Unlock by calling rw.RLock and rw.RUnlock.
WaitGroup
A WaitGroup is a counting semaphore typically used to wait for a group of goroutines or tasks to finish.Adds delta to the WaitGroup counter. If the counter becomes zero, all goroutines blocked on Wait are released. Callers should prefer WaitGroup.Go.
Decrements the WaitGroup counter by one. Equivalent to Add(-1). Callers should prefer WaitGroup.Go.
Blocks until the WaitGroup counter is zero.
Calls f in a new goroutine and adds that task to the WaitGroup. When f returns, the task is removed from the WaitGroup. The function f must not panic.
Once
Once is an object that will perform exactly one action.Calls the function f if and only if Do is being called for the first time for this instance of Once. Multiple calls will only invoke f once.
Cond
Cond implements a condition variable, a rendezvous point for goroutines waiting for or announcing the occurrence of an event.Returns a new Cond with Locker l.
Atomically unlocks c.L and suspends execution of the calling goroutine. After later resuming execution, Wait locks c.L before returning.
Wakes one goroutine waiting on c, if there is any.
Wakes all goroutines waiting on c.
For many simple use cases, users will be better off using channels than a Cond (Broadcast corresponds to closing a channel, and Signal corresponds to sending on a channel).
Pool
A Pool is a set of temporary objects that may be individually saved and retrieved. A Pool is safe for use by multiple goroutines simultaneously.Selects an arbitrary item from the Pool, removes it from the Pool, and returns it to the caller. If Get would otherwise return nil and p.New is non-nil, Get returns the result of calling p.New.
Adds x to the pool.
Optionally specifies a function to generate a value when Get would otherwise return nil.
Map
Map is like a Go map[any]any but is safe for concurrent use by multiple goroutines without additional locking or coordination.Returns the value stored in the map for a key, or nil if no value is present.
Sets the value for a key.
Returns the existing value for the key if present. Otherwise, it stores and returns the given value.
Deletes the value for a key, returning the previous value if any.
Deletes the value for a key.
Swaps the value for a key and returns the previous value if any.
Swaps the old and new values for key if the value stored in the map is equal to old.
Deletes the entry for key if its value is equal to old.
Calls f sequentially for each key and value present in the map. If f returns false, Range stops the iteration.
Deletes all the entries, resulting in an empty Map.
Interfaces
Locker
Best Practices
Use channels for higher-level synchronization
Use channels for higher-level synchronization
For most synchronization needs, channels provide better composability and are easier to reason about than low-level primitives like Mutex and Cond.
Prefer WaitGroup.Go over Add/Done
Prefer WaitGroup.Go over Add/Done
The Go method is simpler and less error-prone than manually managing Add/Done calls. It automatically handles the counter and ensures Done is called even if the function panics.
Never copy sync types after first use
Never copy sync types after first use
All sync types contain internal state that must not be copied. Always pass pointers to these types.
Use defer for Unlock calls
Use defer for Unlock calls
Always defer Unlock calls immediately after Lock to ensure the lock is released even if the function panics.
Pool is for performance optimization only
Pool is for performance optimization only
Use Pool to cache allocated but unused items for later reuse, reducing GC pressure. Don’t use Pool for managing shared state or as a general cache.