go-concurrency-patterns

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

Updated Apr 5, 2026
One-click install
npx skills add https://github.com/RobinMillford/GopherNotebook --skill go-concurrency-patterns-robinmillford
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-concurrency-patterns
Source: https://github.com/RobinMillford/GopherNotebook/tree/main/.claude/skills/go-concurrency-patterns
Command: npx skills add https://github.com/RobinMillford/GopherNotebook --skill go-concurrency-patterns-robinmillford

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires golang.org/x/sync.

What problem does it solve? Writing correct concurrent Go code is error-prone: goroutine leaks, race conditions, deadlocks, and missing cancellation handling are common pitfalls. This Skill provides production-tested patterns for goroutines, channels, synchronization primitives, and context management so you can build concurrent Go applications without reinventing unsafe solutions. ## Core Features & Use Cases - Worker Pools & Pipelines: Implement bounded worker pools, fan-out/fan-in pipelines, and semaphore-based rate limiting with proper context cancellation. - Graceful Shutdown & Error Handling: Coordinate clean shutdowns with signal handling, WaitGroups, and errgroup-based error propagation with automatic cancellation. - Race Debugging Guidance: Apply race detector workflows (go test -race) and best practices to avoid shared-memory bugs and goroutine leaks. - Use Case: You are building an HTTP scraper that must fetch hundreds of URLs concurrently with a concurrency limit and cancel all in-flight requests on the first failure — use the errgroup pattern with SetLimit to implement it correctly. ## Quick Start Ask the assistant to implement a worker pool in Go with context-based cancellation and graceful shutdown for your specific task.

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 jobs channel, spawn a fixed number of goroutines that range over it, and collect results through a results channel closed after a WaitGroup finishes. Add context.Context checks inside workers so cancellation stops processing cleanly.

How to limit concurrency in Go with errgroup?

Use errgroup.WithContext to get a group tied to a cancellable context, then call g.SetLimit(n) to cap concurrent goroutines. The first error returned by any g.Go function cancels the shared context and is returned by g.Wait.

What is the difference between sync.Mutex and channels in Go?

Channels pass ownership of data between goroutines following the share-memory-by-communicating principle, while sync.Mutex protects shared state with mutual exclusion. Prefer channels for coordination and mutexes for guarding small critical sections or counters.

How do I detect race conditions in Go code?

Run tests or binaries with the race detector: go test -race ./... or go run -race main.go. The detector instruments memory accesses at runtime and reports conflicting unsynchronized reads and writes with stack traces.

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. Prevent leaks by passing context.Context, selecting on ctx.Done(), closing channels from the sender side, and ensuring every spawned goroutine has a termination condition.

When should I use sync.Map instead of a regular map with mutex?

Use sync.Map for read-heavy workloads where keys are written once and read many times, or when goroutines access disjoint key sets. For write-heavy workloads, a sharded map with per-shard RWMutex typically performs better.