From golang-skills
Guides writing concurrent Go code: goroutines, channels, mutexes, thread-safety, and avoiding data races. Use when parallelizing work or protecting shared state.
How this skill is triggered — by the user, by Claude, or both
Slash command
/golang-skills:go-concurrencyThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Compatibility: Atomic examples may use standard-library typed atomics where available or `go.uber.org/atomic` where a project already depends on it.
Compatibility: Atomic examples may use standard-library typed atomics where available or
go.uber.org/atomicwhere a project already depends on it.
references/GOROUTINE-PATTERNS.md - Read when starting, stopping, or waiting for goroutines.references/SYNC-PRIMITIVES.md - Read when choosing between mutexes, atomics, channels, and once-like primitives.references/BUFFER-POOLING.md - Read when considering channel-backed or sync.Pool-style reuse.references/ADVANCED-PATTERNS.md - Read for worker pools, pipelines, errgroup, and cancellation-heavy patterns.Normative: When you spawn goroutines, make it clear when or whether they exit.
Goroutines can leak by blocking on channel sends/receives. The GC will not terminate a blocked goroutine even if no other goroutine holds a reference to the channel. Even non-leaking in-flight goroutines cause panics (send on closed channel), data races, memory issues, and resource leaks.
init() — expose lifecycle methods (Close, Stop,
Shutdown) instead// Good: Clear lifetime with WaitGroup.Go (Go 1.25+)
var wg sync.WaitGroup
for item := range queue {
item := item
wg.Go(func() { process(ctx, item) })
}
wg.Wait()
// Bad: No way to stop or wait
go func() { for { flush(); time.Sleep(delay) } }()
Test for leaks with go.uber.org/goleak.
Principle: Never start a goroutine without knowing how it will stop.
"Do not communicate by sharing memory; instead, share memory by communicating."
This is Go's foundational concurrency design principle. Use channels for ownership transfer and orchestration — when one goroutine produces a value and another consumes it. Use mutexes when multiple goroutines access shared state and channels would add unnecessary complexity.
Default to channels. Fall back to sync.Mutex / sync.RWMutex when the
problem is naturally about protecting a shared data structure (e.g., a cache or
counter) rather than passing data between goroutines.
Normative: Prefer synchronous functions over asynchronous ones.
| Benefit | Why |
|---|---|
| Localized goroutines | Lifetimes easier to reason about |
| Avoids leaks and races | Easier to prevent resource leaks and data races |
| Easier to test | Check input/output without polling |
| Caller flexibility | Caller adds concurrency when needed |
Advisory: It is quite difficult (sometimes impossible) to remove unnecessary concurrency at the caller side. Let the caller add concurrency when needed.
The zero-value of sync.Mutex and sync.RWMutex is valid — almost never need
a pointer to a mutex.
// Good: Zero-value is valid // Bad: Unnecessary pointer
var mu sync.Mutex mu := new(sync.Mutex)
Don't embed mutexes — use a named mu field to keep Lock/Unlock as
implementation details, not exported API.
Normative: Specify channel direction where possible.
Direction prevents errors (compiler catches closing a receive-only channel), conveys ownership, and is self-documenting.
func produce(out chan<- int) { /* send-only */ }
func consume(in <-chan int) { /* receive-only */ }
func transform(in <-chan int, out chan<- int) { /* both */ }
Channels should have size zero (unbuffered) or one. Any other size requires justification for:
c := make(chan int) // unbuffered — Good
c := make(chan int, 1) // size one — Good
c := make(chan int, 64) // arbitrary — needs justification
Use atomic.Bool, atomic.Int64, etc. (stdlib sync/atomic since Go 1.19, or
go.uber.org/atomic) for type-safe
atomic operations. Raw int32/int64 fields make it easy to forget atomic
access on some code paths.
// Good: Type-safe // Bad: Easy to forget
var running atomic.Bool var running int32 // atomic
running.Store(true) atomic.StoreInt32(&running, 1)
running.Load() running == 1 // race!
Advisory: Document thread-safety when it's not obvious from the operation type.
Go users assume read-only operations are safe for concurrent use, and mutating operations are not. Document concurrency when:
Lookup that mutates LRU stateFor context.Context guidance (parameter placement, struct storage, custom types, derivation patterns), see the dedicated go-context skill.
Use a buffered channel as a free list to reuse allocated buffers. This "leaky
buffer" pattern uses select with default for non-blocking operations.
npx claudepluginhub cxuu/golang-skills --plugin golang-skillsGuides writing, reviewing, and auditing concurrent Go code using goroutines, channels, select, locks, sync primitives, errgroup, singleflight, and worker pools. Detects leaks, races, and ownership issues.
Provides concurrency patterns for Go — goroutines, channels, sync primitives, errgroup, worker pools, pipelines. Reviews concurrent code for leaks, race conditions, and channel ownership issues.
Provides code examples for Go goroutines, channels (unbuffered, buffered, directional, closing), select statements, and sync patterns when writing concurrent Go code.