Loading...
Loading...
Golang concurrency patterns. Use when writing or reviewing concurrent Go code involving goroutines, channels, select, locks, sync primitives, errgroup, singleflight, worker pools, or fan-out/fan-in pipelines. Also triggers when you detect goroutine leaks, race conditions, channel ownership issues, or need to choose between channels and mutexes.
npx skill4agent add samber/cc-skills-golang golang-concurrencyCommunity default. A company skill that explicitly supersedesskill takes precedence.samber/cc-skills-golang@golang-concurrency
chan<-<-chanctx.Done()time.Aftertime.NewTimerResetgo.uber.org/goleak| Scenario | Use | Why |
|---|---|---|
| Passing data between goroutines | Channel | Communicates ownership transfer |
| Coordinating goroutine lifecycle | Channel + context | Clean shutdown with select |
| Protecting shared struct fields | | Simple critical sections |
| Simple counters, flags | | Lock-free, lower overhead |
| Many readers, few writers on a map | | Optimized for read-heavy workloads. Concurrent map read/write causes a hard crash |
| Caching expensive computations | | Execute once or deduplicate |
| Need | Use | Why |
|---|---|---|
| Wait for goroutines, errors not needed | | Fire-and-forget |
| Wait + collect first error | | Error propagation |
| Wait + cancel siblings on first error | | Context cancellation on error |
| Wait + limit concurrency | | Built-in worker pool |
| Primitive | Use case | Key notes |
|---|---|---|
| Protect shared state | Keep critical sections short; never hold across I/O |
| Many readers, few writers | Never upgrade RLock to Lock (deadlock) |
| Simple counters, flags | Prefer typed atomics (Go 1.19+): |
| Concurrent map, read-heavy | No explicit locking; use |
| Reuse temporary objects | Always |
| One-time initialization | Go 1.21+: |
| Wait for goroutine completion | |
| Deduplicate concurrent calls | Cache stampede prevention |
| Goroutine group + errors | |
context.Contextsync.WaitGrouperrgroupsamber/rogo funcgo methodtime.Afterctx.Done()sync.Map| Mistake | Fix |
|---|---|
| Fire-and-forget goroutine | Provide stop mechanism (context, done channel) |
| Closing channel from receiver | Only the sender closes |
| Reuse |
Missing | Always select on context to allow cancellation |
| Unbounded goroutine spawning | Use |
| Sharing pointer via channel | Send copies or immutable values |
| Call |
Forgetting | Always run |
| Mutex held across I/O | Keep critical sections short |
samber/cc-skills-golang@golang-performancesync.Poolsamber/cc-skills-golang@golang-contextsamber/cc-skills-golang@golang-safetysamber/cc-skills-golang@golang-troubleshootingsamber/cc-skills-golang@golang-design-patterns