agentleFS
narenaryannarenaryan/agent-skills/skills/go/go-concurrency-patterns/SKILL.md

go-concurrency-patterns

Skill0 starsChanged 39 days agoMITView on GitHub

Use when designing Go concurrent code — goroutine lifetimes, channel sizing, worker pools, parallelization, or diagnosing goroutine leaks and data races; covers semaphore via buffered channel, leaky buffer, fan-out, channel-of-channels for RPC

11 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Go Concurrency Patterns

Ownership transfers on send: one goroutine owns a value at a time. Unbuffered = sync point; buffered = bounded decoupling.

Channel blocking

OpUnbufferedBuffered (N)
sendblocks until recv readyblocks when full
recvblocks until sender readyblocks when empty
send on closedpanicspanics
recv on closedzero, ok=falsedrain, zero, ok=false

Semaphore via buffered channel

var sem = make(chan struct{}, MaxOutstanding)
func handle(r *Request) {
    sem <- struct{}{}
    defer func() { <-sem }()
    process(r)
}

Replaces mutex+counter. struct{} is zero-byte.

Non-blocking select (leaky buffer)

select {
case b = <-freeList:   // try pool
default:
    b = new(Buffer)    // allocate fallback
}
...
select {
case freeList <- b:    // try return
default:               // drop; GC reclaims
}

default: makes select non-blocking.

Channel-of-channels for replies

type Request struct {
    args  []int
    reply chan int
}
queue <- &Request{args, make(chan int)}
ans := <-req.reply

Each request carries its own reply channel — no correlation IDs.

Fan-out across cores

n := runtime.GOMAXPROCS(0)
c := make(chan int, n)         // buffered for drain
for i := 0; i < n; i++ { go worker(i, c) }
for i := 0; i < n; i++ { <-c } // join

Buffer size = worker count so the last worker never blocks.

Pitfalls

  • Loop-variable capture (Go <1.22): for req := range q { go f(req) } — all goroutines see the final req. Pass as arg: go func(r *Req){ f(r) }(req), or upgrade to 1.22+.
  • Goroutine leak: go func(){ c <- 1 }() with no receiver blocks forever; the goroutine outlives its caller. Size buffers correctly or guarantee a receiver before sending.
  • Closing from the receiver side: sending on a closed channel panics. Only the sole sender may close; with multiple senders use a separate done channel or sync.Once.
  • Unbuffered self-deadlock: sending and receiving on the same unbuffered channel in one goroutine deadlocks instantly.
  • Select starvation: select chooses randomly among ready cases. A default: branch disables blocking entirely — don't include one when you actually want to wait.

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.