go-concurrency

Guides writing and reviewing concurrent Go code with goroutines, channels, and sync primitives.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-concurrency-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-concurrency
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-concurrency
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-concurrency-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Concurrent Go code is prone to goroutine leaks, race conditions, deadlocks, and unclear channel ownership. This Skill enforces an ownership-first discipline so every goroutine has a defined exit path, stop signal, and wait mechanism, and every channel has a clear owner. ## Core Features & Use Cases - Primitive selection guidance: Decision tables for choosing between channels, mutexes, atomics, sync.Map, sync.Pool, sync.Once, WaitGroup, errgroup, and singleflight based on the scenario. - Write and review workflows: Step-by-step checklists for writing concurrent code (plan exits before go, assert channel ownership, select on ctx.Done()) and for auditing diffs (scan spawns, check shared state, audit channels). - Deep reference material: Detailed patterns for channels and select, pipelines with fan-out/fan-in, bounded worker pools via errgroup.SetLimit, and sync primitives including Go 1.21+ OnceValue and Go 1.24 wg.Go(). - Use Case: When reviewing a pull request that introduces goroutines, use this Skill to verify each spawn has an exit strategy, no time.After leaks in loops, and the race detector passes with go test -race ./.... ## Quick Start Review my Go service's worker pool implementation for goroutine leaks, race conditions, and channel ownership issues.

Frequently Asked Questions about go-concurrency

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent goroutine leaks in Go?

Give every goroutine three answers before spawning: how it exits, how to stop it, and how to wait for it. Pass a context and select on ctx.Done() in every loop, then verify with goleak.VerifyTestMain in TestMain to catch leaks across tests.

When should I use a channel vs a mutex in Go?

Use channels to pass data between goroutines and coordinate lifecycle, since they communicate ownership transfer. Use sync.Mutex or sync.RWMutex to protect shared struct fields, and sync/atomic for simple counters and flags where lock-free operations suffice.

What is the difference between WaitGroup and errgroup?

Use sync.WaitGroup when you only need to wait for goroutines to finish. Use errgroup.Group to collect the first error, errgroup.WithContext to cancel sibling goroutines on failure, and errgroup.SetLimit to bound concurrency instead of hand-rolled worker pools.

Why is time.After in a loop a problem in Go?

time.After creates a new timer on every iteration that cannot be stopped, leaking resources until each timer fires. Reuse a single time.NewTimer with Stop and Reset inside the loop instead.

Who should close a channel in Go, sender or receiver?

Only the sender (producer) closes a channel; a receiver closing it can panic if the sender writes afterward. Declare direction in signatures with chan<- and <-chan so the compiler enforces correct usage.

When should I use sync.Map instead of a mutex-guarded map?

Use sync.Map only for write-once/read-many patterns or disjoint key sets across goroutines. When writes are frequent, keys overlap, or you need iteration and length, a plain map guarded by sync.RWMutex is faster.