go-concurrency-patterns

Implements Go concurrency patterns using goroutines, channels, sync primitives, and context.

Updated Nov 10, 2013
One-click install
npx skills add https://github.com/bnferguson/dotfiles --skill go-concurrency-patterns-bnferguson
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: go-concurrency-patterns
Source: https://github.com/bnferguson/dotfiles/tree/main/.agents/skills/go-concurrency-patterns
Command: npx skills add https://github.com/bnferguson/dotfiles --skill go-concurrency-patterns-bnferguson

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires golang.org/x/sync, and includes references (resource) components.

What problem does it solve? Writing correct concurrent Go code is error-prone: goroutine leaks, race conditions, deadlocks, and improper channel handling cause subtle production bugs. This Skill provides production-tested patterns and best practices for building concurrent Go applications correctly. ## Core Features & Use Cases - Seven Production Patterns: Worker pools, fan-in pipelines, bounded concurrency with semaphores, graceful shutdown, errgroup error handling, concurrent maps (sync.Map and sharded), and select timeout/priority patterns. - Race Detection Guidance: Commands for running tests and builds with Go's race detector to catch data races early. - Best Practices Checklist: Concrete do's and don'ts covering channel closing, context cancellation, and goroutine lifecycle management. - Use Case: You need to process thousands of jobs concurrently with a rate limit and clean shutdown on SIGTERM. Load this Skill to get a worker pool with semaphore-based bounding and signal-driven graceful drain. ## Quick Start Ask the AI to implement a worker pool in Go with graceful shutdown and context cancellation using this Skill's patterns.

Frequently Asked Questions about go-concurrency-patterns

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

FAQPage Schema
How do I implement a worker pool in Go?▼

Create a shared jobs channel, spawn a fixed number of goroutines that range over it, and use a sync.WaitGroup to close the results channel when all workers finish. Check ctx.Done() inside each worker so cancellation stops processing cleanly.

How to limit concurrency in Go with a semaphore?▼

Use golang.org/x/sync/semaphore with a weighted limit: call Acquire before starting each task and Release when it finishes. A buffered channel of struct{} works as a lightweight alternative for simple cases.

When should I use sync.Map vs a sharded map in Go?▼

Use sync.Map for read-heavy workloads with infrequent writes, since it optimizes the read path. For write-heavy workloads, use a sharded map that distributes keys across multiple RWMutex-protected maps to reduce lock contention.

How do I detect race conditions in Go code?▼

Run go test -race ./... to execute tests with the race detector, or build and run binaries with go build -race and go run -race. The detector reports unsynchronized memory accesses between goroutines at runtime.

Why do goroutines leak and how do I prevent it?▼

Goroutines leak when they block forever on channel sends or receives with no exit path. Always provide cancellation via context.Context, close channels from the sender side, and ensure every spawned goroutine can observe ctx.Done().

What is errgroup and when should I use it?▼

errgroup from golang.org/x/sync runs a group of goroutines and returns the first error, cancelling a shared context for the rest. Use it for concurrent operations like parallel HTTP fetches where one failure should abort the batch; SetLimit caps concurrency.