golang-concurrency

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

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-concurrency-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-concurrency
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-concurrency
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-concurrency-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Concurrent Go code is prone to goroutine leaks, race conditions, channel ownership bugs, and deadlocks that are hard to detect in review and expensive in production. This Skill encodes Go concurrency best practices so AI coding agents write and review concurrent code correctly the first time. ## Core Features & Use Cases - Write mode: Implements goroutines, channels, select loops, worker pools, and fan-out/fan-in pipelines following structured concurrency rules (clear exit paths, context cancellation, sender-closes-channel). - Review mode: Audits PR diffs for goroutine leaks, missing ctx.Done() in select, unprotected shared state, and incorrect WaitGroup usage. - Audit mode: Orchestrates up to 5 parallel sub-agents to scan a large codebase for concurrency anti-patterns. - Use Case: When asked to fetch 50 URLs concurrently with a limit of 10 and fail-fast behavior, it produces errgroup.WithContext plus SetLimit(10) instead of a hand-rolled worker pool. ## Quick Start Ask the agent to write or review concurrent Go code, for example: review this Go function that spawns goroutines and check it for leaks and race conditions.

Frequently Asked Questions about golang-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 a clear exit path: pass a context.Context and include a ctx.Done() case in every select loop, and let callers wait via sync.WaitGroup or errgroup. In tests, use go.uber.org/goleak in TestMain to detect leaked goroutines automatically.

When should I use errgroup instead of sync.WaitGroup?

Use errgroup when goroutines return errors, need cancellation of siblings on first failure, or need bounded concurrency via SetLimit. Use sync.WaitGroup only for simple fire-and-wait work with no error propagation; Go 1.25+ offers wg.Go for that case.

Should I use channels or mutexes for shared state in Go?

Use channels to transfer ownership of data between goroutines, and sync.Mutex or sync.RWMutex to protect shared struct fields with short critical sections. For simple counters and flags, prefer typed atomics like atomic.Int64 and atomic.Bool.

When is sync.Map the right choice over RWMutex and a map?

sync.Map fits write-once/read-many caches or disjoint key sets accessed by many goroutines. For write-heavy workloads with overlapping keys, a plain map guarded by sync.RWMutex is faster, and unsynchronized concurrent map access causes a hard crash.

Why should I avoid time.After inside a select loop?

Each time.After call allocates a new timer, causing churn in hot loops. Create one time.NewTimer outside the loop and call Reset after each event; on Go versions before 1.23, drain the channel if Stop reports a pending value.

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

Only the sender closes a channel; closing from the receiver panics if the sender writes afterward. Receivers signal completion through a separate done channel or context cancellation, and producers select on that signal alongside their sends.