go-concurrency-patterns

📁 caijiatao/skills 📅 1 day ago
3
总安装量
2
周安装量
#61334
全站排名
安装命令
npx skills add https://github.com/caijiatao/skills --skill go-concurrency-patterns

Agent 安装分布

opencode 2
kilo 2
gemini-cli 2
claude-code 2
github-copilot 2
codex 2

Skill 文档

Go Concurrency Patterns

Production patterns for Go concurrency including goroutines, channels, synchronization primitives, and context management.

When to Use This Skill

  • Building concurrent Go applications
  • Implementing worker pools and pipelines
  • Managing goroutine lifecycles
  • Using channels for communication
  • Debugging race conditions
  • Implementing graceful shutdown

Core Concepts

Primitives

Primitive Purpose Reference
goroutine Lightweight concurrent execution core-goroutines
channel Communication between goroutines core-channels
select Multiplex channel operations core-channels
sync.Mutex Mutual exclusion core-sync
sync.WaitGroup Wait for goroutines to complete core-sync
context.Context Cancellation and deadlines core-context

Patterns

Pattern Description Reference
Worker Pool Fixed number of workers processing jobs patterns-worker-pool
Pipeline Fan-out/fan-in for data flow patterns-pipeline
Graceful Shutdown Clean shutdown with signal handling patterns-graceful-shutdown
Error Group Concurrent error handling with errgroup patterns-errgroup

The Go Concurrency Mantra

Don't communicate by sharing memory;
share memory by communicating.

Quick Reference

Spawning a Goroutine

go func() {
    // Do work concurrently
}()

Creating a Channel

ch := make(chan int)     // Unbuffered
ch := make(chan int, 10) // Buffered

Waiting for Goroutines

var wg sync.WaitGroup
wg.Add(1)
go func() {
    defer wg.Done()
    // Work
}()
wg.Wait()

Context Cancellation

ctx, cancel := context.WithCancel(context.Background())
go func() {
    <-ctx.Done()
    // Cleanup
}()
cancel() // Signal shutdown

Race Detection

# Run tests with race detector
go test -race ./...

# Build with race detector
go build -race .

Best Practices

  • Do: Use context for cancellation
  • Do: Close channels from sender side only
  • Do: Use errgroup for concurrent operations
  • Do: Buffer channels when you know the count
  • Don’t: Leak goroutines – always have exit path
  • Don’t: Close channels from receiver
  • Don’t: Use shared memory unless necessary
  • Don’t: Ignore context cancellation

Resources