go-concurrency-patterns

Master Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "go-concurrency-patterns" with this command: npx skills add caijiatao/skills/caijiatao-skills-go-concurrency-patterns

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

PrimitivePurposeReference
goroutineLightweight concurrent executioncore-goroutines
channelCommunication between goroutinescore-channels
selectMultiplex channel operationscore-channels
sync.MutexMutual exclusioncore-sync
sync.WaitGroupWait for goroutines to completecore-sync
context.ContextCancellation and deadlinescore-context

Patterns

PatternDescriptionReference
Worker PoolFixed number of workers processing jobspatterns-worker-pool
PipelineFan-out/fan-in for data flowpatterns-pipeline
Graceful ShutdownClean shutdown with signal handlingpatterns-graceful-shutdown
Error GroupConcurrent error handling with errgrouppatterns-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

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

golang-code-review

No summary provided by upstream source.

Repository SourceNeeds Review
General

effective-go

No summary provided by upstream source.

Repository SourceNeeds Review
General

golang-testing

No summary provided by upstream source.

Repository SourceNeeds Review
Web3

compact-state

Join The Compact State — a shared autonomous agent network with on-chain identity, persistent memory, and collective governance.

Archived Source