mcpbeat Sign in

Go Concurrency Agent Skill

by cxuu

Use when writing concurrent Go code — goroutines, channels, mutexes, or thread-safety guarantees. Also use when parallelizing work, fixing data races, or protecting shared state, even if the user doesn't explicitly mention concurrency primitives. Does not cover context.Context patterns (see go-context).

5k tokens
context cost
the whole folder, loaded on every use
5
files
instructions only
0
copies elsewhere
how many repositories repackaged it
136
stars on the repo
on the repository, not the skill itself

Install

one command, takes just this skill from the repository
npx skills add https://github.com/cxuu/golang-skills --skill go-concurrency

The instruction itself

15 sections, as written by the author

Go Concurrency

> Compatibility: Atomic examples may use standard-library typed atomics where available or go.uber.org/atomic where a project already depends on it.

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.
  • 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.

Goroutine Lifetimes

> 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.

Core Rules

  • Every goroutine needs a stop mechanism — a predictable end time, a

cancellation signal, or both

  • Code must be able to wait for the goroutine to finish
  • No goroutines in init() — expose lifecycle methods (Close, Stop,

Shutdown) instead

  • Keep synchronization scoped — constrain to function scope, factor logic

into synchronous functions

// 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.


Share by Communicating

> "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.


Synchronous Functions

> 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.


Zero-value Mutexes

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.


Channel Direction

> 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 */ }

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
c := make(chan int)    // unbuffered — Good
c := make(chan int, 1) // size one — Good
c := make(chan int, 64) // arbitrary — needs justification

Atomic Operations

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!

Documenting Concurrency

> 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:

  • Read vs mutating is unclear — e.g., a Lookup that mutates LRU state
  • API provides synchronization — e.g., thread-safe clients
  • Interface has concurrency requirements — document in type definition

Context Usage

> For context.Context guidance (parameter placement, struct storage, custom

> types, derivation patterns), see the dedicated

> go-context skill.


Buffer Pooling with Channels

Use a buffered channel as a free list to reuse allocated buffers. This "leaky

buffer" pattern uses select with default for non-blocking operations.


  • Context propagation: See go-context when passing cancellation, deadlines, or request-scoped values through goroutines
  • Error handling: See go-error-handling when propagating errors from goroutines or using errgroup
  • Defensive hardening: See go-defensive when protecting shared state at API boundaries or using defer for cleanup
  • Interface design: See go-interfaces when choosing receiver types for types with sync primitives

External Resources

  • [Never start a goroutine without knowing how it will

stop](https://dave.cheney.net/2016/12/22/never-start-a-goroutine-without-knowing-how-it-will-stop)

— Dave Cheney

  • [Rethinking Classical Concurrency

Patterns](https://www.youtube.com/watch?v=5zXAHh5tJqQ) — Bryan Mills

(GopherCon 2018)

detector for testing

atomic operations

Other skills for the same job

different authors, same section of the catalogue
Changelog Generator
by frostant
×9

Automatically creates user-facing changelogs from git commits by analyzing commit history, categorizing changes, and transforming technical commits into clear, customer-friendly release notes. Turns hours of manual changelog writing into minutes of automated generation.

774 tokens
Codex
by softaworks
×2

Use when the user asks to run Codex CLI (codex exec, codex resume) or references OpenAI Codex for code analysis, refactoring, or automated editing. Uses GPT-5.2 by default for state-of-the-art software engineering.

2k tokens
Memory Safety Patterns
by ComeOnOliver
×2

Implement memory-safe programming with RAII, ownership, smart pointers, and resource management across Rust, C++, and C. Use when writing safe systems code, managing resources, or preventing memory bugs.

6k tokens
Pysam
by K-Dense-AI
×1

Python/HTSlib workflows for genomic files. Use when reading, querying, filtering, or writing SAM/BAM/CRAM, VCF/BCF, FASTA/FASTQ, or tabix data with pysam, including pileup, coverage, indexing, and CRAM references.

34k tokens scripts
Scientific Critical Thinking
by K-Dense-AI
×1

Evaluate scientific claims and evidence quality. Use for assessing experimental design validity, identifying biases and confounders, applying evidence grading frameworks (GRADE, Cochrane Risk of Bias), or teaching critical analysis. Best for understanding evidence quality, identifying flaws. For formal peer review writing use peer-review.

26k tokens
Gh Fix CI
by openai
vendor ×1

Use when a user asks to debug or fix failing GitHub PR checks that run in GitHub Actions; use `gh` to inspect checks and logs, summarize failure context, draft a fix plan, and implement only after explicit approval. Treat external providers (for example Buildkite) as out of scope and report only the details URL.

8k tokens scripts
Declarative Agent Developer
by microsoft
vendor ×1

> Create, build, deploy, and localize declarative agents for M365 Copilot and Teams. USE THIS SKILL for ANY task involving a declarative agent — including localization, scaffolding, editing manifests, adding capabilities, and deploying. Localization requires tokenized manifests and language files that only this skill knows how to produce. "scaffold an agent", "new agent project", "add a capability", "add a plugin", "configure my agent", "deploy my agent", "fix my agent manifest", "edit my agent", "localize my agent", "add localization", "translate my agent", "multi-language agent", "add an API plugin", "add an MCP plugin", "add OAuth to my plugin", "review instructions", "improve instructions", "fix my instructions"

66k tokens
Documentation
by lingxling
×1

Documentation generation workflow covering API docs, architecture docs, README files, code comments, and technical writing.

1k tokens

How to use it

Copy the folder

Take cxuu/go-concurrency from the repository into ~/.claude/skills for personal use, or into .claude/skills inside a project.

Check the name does not clash

The agent identifies a skill by the name field in its header. Two skills with the same name cannot sit side by side — one of them will be ignored.