go-concurrency-patterns

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

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill go-concurrency-patterns-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-concurrency-patterns
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/go-concurrency-patterns
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill go-concurrency-patterns-sanketadlak

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 logic cause production bugs. This Skill provides proven patterns for worker pools, pipelines, graceful shutdown, and error handling so you avoid reinventing unsafe concurrency code. ## Core Features & Use Cases - Worker Pools & Pipelines: Implement fan-out/fan-in pipelines and bounded worker pools with channels and sync.WaitGroup. - Cancellation & Shutdown: Use context.Context, errgroup, and signal handling for timeouts, cancellation, and graceful shutdown. - Synchronization Primitives: Apply sync.Mutex, sync.Map, sharded maps, and semaphores for shared-state and rate-limited workloads. - Use Case: You need to fetch 500 URLs concurrently with a limit of 10 simultaneous requests and cancel everything on the first error. Use the errgroup pattern with SetLimit to get bounded, cancellable concurrency. ## Quick Start Ask the AI to implement a worker pool in Go with context-based cancellation and graceful shutdown for your job processing code.

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. Use sync.WaitGroup to wait for workers and close the results channel from a separate goroutine after wg.Wait() completes.

How to limit concurrent goroutines in Go?

Use golang.org/x/sync/semaphore with a weighted limit, or a buffered channel acting as a semaphore where each goroutine acquires a slot before running. errgroup's SetLimit method also caps concurrent goroutines directly.

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

Use sync.Map for read-heavy workloads with infrequent writes or disjoint key sets across goroutines. For write-heavy workloads, a sharded map with per-shard RWMutex reduces lock contention better than a single mutex.

How do I detect race conditions in Go code?

Run tests or builds with the -race flag, such as go test -race ./... or go run -race main.go. The race detector instruments memory accesses and reports conflicting unsynchronized reads and writes 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. Prevent leaks by passing context.Context, selecting on ctx.Done(), and ensuring the sender closes channels so receivers terminate.