| name | go-concurrency |
| description | Use when writing, reviewing, or debugging concurrent Go code. Covers goroutine lifecycle management, channels, errgroup, mutexes, atomics, sync.Map, and synchronous-first design. Based on Google and Uber style guides. |
| version | 2.0.0 |
| license | MIT |
| metadata | {"author":"saisudhir14","tags":"golang go concurrency goroutines channels mutex atomic errgroup","category":"languages"} |
Go Concurrency
Patterns for safe, efficient concurrent Go code.
Channel Size
Channels should have size zero (unbuffered) or one. Any other size requires justification about what prevents filling under load.
c := make(chan int, 64)
c := make(chan int)
c := make(chan int, 1)
Goroutine Lifetimes
Document when and how goroutines exit. Goroutines blocked on channels will not be garbage collected even if the channel is unreachable.
func (w *Worker) Run(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case job := <-w.jobs:
w.process(job)
}
}
}
Use errgroup for Concurrent Operations
Prefer errgroup.Group over manual sync.WaitGroup for error-returning goroutines.
import "golang.org/x/sync/errgroup"
func processItems(ctx context.Context, items []Item) error {
g, ctx := errgroup.WithContext(ctx)
for _, item := range items {
g.Go(func() error {
return process(ctx, item)
})
}
return g.Wait()
}
func processItemsLimited(ctx context.Context, items []Item) error {
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(10)
for _, item := range items {
g.Go(func() error {
return process(ctx, item)
})
}
return g.Wait()
}
Prefer Synchronous Functions
Synchronous functions are easier to reason about and test. Let callers add concurrency when needed.
func Fetch(url string) <-chan Result
func Fetch(url string) (Result, error)
Zero Value Mutexes
The zero value of sync.Mutex is valid. Do not use pointers to mutexes or embed them in exported structs.
mu := new(sync.Mutex)
type SMap struct {
sync.Mutex
data map[string]string
}
type SMap struct {
mu sync.Mutex
data map[string]string
}
Atomic Operations (Go 1.19+)
Use the standard library's typed atomics.
import "sync/atomic"
type Counter struct {
value atomic.Int64
}
func (c *Counter) Inc() { c.value.Add(1) }
func (c *Counter) Value() int64 { return c.value.Load() }
sync.Map Performance (Go 1.24+)
The sync.Map implementation was significantly improved in Go 1.24. Modifications of disjoint sets of keys are much less likely to contend on larger maps, and there is no longer any ramp-up time required to achieve low-contention loads.
Context Cancellation
Always select on ctx.Done() in long-running goroutines to allow clean cancellation.
func longOperation(ctx context.Context) error {
resultCh := make(chan result, 1)
go func() {
resultCh <- expensiveWork()
}()
select {
case <-ctx.Done():
return ctx.Err()
case r := <-resultCh:
return r.err
}
}
For graceful server shutdown patterns, see references/patterns.md.