agentleFS
h0rn3th0rn3t/golang-skills/skills/go-concurrency/SKILL.md

go-concurrency

Skill7 starsChanged 3 days agoApache-2.0View on GitHub

Use when writing concurrent Go code with goroutines, channels, mutexes, timers, or tickers; parallelizing work; fixing data races; or protecting shared state. Context cancellation belongs to go-context.

9 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 8 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use atomic.Bool, atomic.Int64, etc.

    line 118

“Common” is measured: kept by at least 6 in 10 of the 8 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Go Concurrency

Compatibility: Baseline Go 1.27 (see COMPATIBILITY.md). sync.WaitGroup.Go and testing/synctest require Go 1.25+; typed atomics (atomic.Bool) are stdlib since Go 1.19; the goroutineleak profile follows the toolchain that builds the binary, not the go directive (Go 1.27 toolchain).

Resource Routing

  • 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, or when code stops, resets, or drains a timer.
  • references/BUFFER-POOLING.md - Read when considering channel-backed or sync.Pool-style reuse.
  • references/ADVANCED-PATTERNS.md - Read for bounded errgroup work, cooperative cancellation, request/reply channels, and CPU-bound parallelization.

Goroutine Lifetimes

Normative: When you spawn goroutines, make it clear when or whether they exit.

The GC will not terminate a blocked goroutine even if no other goroutine holds a reference to the channel.

Core Rules

  1. Every goroutine needs a stop mechanism — a predictable end time, a cancellation signal, or both
  2. Code must be able to wait for the goroutine to finish
  3. No goroutines in init() — expose lifecycle methods (Close, Stop, Shutdown) instead
  4. Keep synchronization scoped — constrain to function scope, factor logic into synchronous functions
  5. Bound fan-out — errgroup.Group.SetLimit(n) when go.mod already requires golang.org/x/sync, otherwise a buffered-channel semaphore with wg.Go; never one goroutine per element of an unbounded input
// Good: a fixed pair of tasks, both joined before returning
var wg sync.WaitGroup
wg.Go(func() { process(ctx, first) })
wg.Go(func() { process(ctx, second) })
wg.Wait()

process must honor ctx and must not panic; WaitGroup.Go does not propagate errors or cancel tasks. For variable-size input or sibling cancellation on error, use the bounded errgroup pattern in references/ADVANCED-PATTERNS.md.

Test for leaks with synctest.Test (Go 1.25+), which fails when a goroutine started in its bubble is still durably blocked once the test function returns: on a channel made in the bubble, sync.Cond.Wait, a bubble WaitGroup.Wait, or time.Sleep. A goroutine stuck in Mutex.Lock, in I/O, or on a channel made outside the bubble makes Test hang until -timeout instead. Outside a bubble, read the goroutineleak profile in the tested process — Go 1.27 toolchain (1.26 needs GOEXPERIMENT=goroutineleakprofile); DIAGNOSTIC-TOOLS.md has the nil-guarded form and what it misses. Keep go.uber.org/goleak where the project already uses it; do not add the module for code a bubble can host.

Share by Communicating

"Do not communicate by sharing memory; instead, share memory by communicating."

Pick the primitive by the shape of the problem, not by preference:

Do you need a goroutine at all?
├─ No — a return value or a synchronous call does it → no goroutine, no channel
└─ Yes
   ├─ Handing a value or ownership to another goroutine → channel
   ├─ Protecting shared state (cache, counter, map)      → sync.Mutex / RWMutex; atomic.* for one word
   ├─ Fan-out that must be bounded                       → errgroup SetLimit (x/sync in go.mod) or wg.Go + semaphore
   └─ Both fit                                           → the one with fewer lines

A goroutine whose caller immediately waits for it is a function call with extra steps — it is on the hunt list in go-code-refactor.

Synchronous Functions

Normative: Prefer synchronous functions over asynchronous ones.

Mutexes

Don't embed mutexes — use a named mu field to keep Lock/Unlock as implementation details, not exported API.

No recursive RLock. A method holding mu.RLock never calls one that takes mu.RLock again: once a writer waits in Lock, the second RLock blocks behind it and neither proceeds. Split the body into an unlocked …Locked variant that both public methods call.

Channel Direction

Normative: Specify channel direction where possible.

Channel Size: One or None

Channels should have size zero (unbuffered) or one. Any other size requires justification for:

  • How the size was determined
  • What prevents the channel from filling under load
  • What happens when writers block

A semaphore channel (make(chan struct{}, n) for the bounded fan-out above) is not a buffer: n is the concurrency limit, and that is its justification.

Atomic Operations

Use atomic.Bool, atomic.Int64, etc. from the standard sync/atomic package for type-safe atomic operations. Keep go.uber.org/atomic when already used by the project or when a required operation is absent from the standard library.

Related Skills

  • go-resilience: bulkhead admission, backpressure, rate scope, recovery budgets.
  • go-context: deriving contexts, deadlines, cancel causes, request-scoped values.
  • go-error-handling: errors from goroutines, errgroup.
  • go-defensive: shared state at API boundaries, defer for cleanup.
  • go-interfaces: receiver types for types holding sync primitives.
  • go-troubleshooting: goroutine dumps, race reports, the symptom catalog when the cause is unknown.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.