Skills · Coding

Go Concurrency Patterns

Unverified30/40

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

Originally by wshobson · MIT

Claude CodePartialHas SKILL.md but declares no allowed-tools — Claude Code will ask for permission each time
CursorPartialPlain prose you can paste in — but no Cursor rules file
CodexPartialPlain prose you can paste in — but no AGENTS.md
Gemini CLIPartialPlain prose you can paste in
CopilotPartialPlain prose you can paste in — but no Copilot instructions file
npx agentalley add go-concurrency-patterns

This command does not work yet — the CLI is still being built. Until then, use Raw in the reader below to take the file.

Who is stuck, and on what

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

The whole source

No sign-in, no blur, nothing truncated
go-concurrency-patterns/SKILL.md108 lines2.7 KBRawView on GitHub
Frontmatter — 2 properties
namego-concurrency-patterns
descriptionMaster Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.
1---
2name: go-concurrency-patterns
3description: Master Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.
4---A5No allowed-tools declared — no way to tell what this skill may touch
5 
6# Go Concurrency Patterns
7 
8Production patterns for Go concurrency including goroutines, channels, synchronization primitives, and context management.
9 
10## When to Use This Skill
11 
12- Building concurrent Go applications
13- Implementing worker pools and pipelines
14- Managing goroutine lifecycles
15- Using channels for communication
16- Debugging race conditions
17- Implementing graceful shutdown
18 
19## Core Concepts
20 
21### 1. Go Concurrency Primitives
22 
23| Primitive | Purpose |
24| ----------------- | -------------------------------- |
25| `goroutine` | Lightweight concurrent execution |
26| `channel` | Communication between goroutines |
27| `select` | Multiplex channel operations |
28| `sync.Mutex` | Mutual exclusion |
29| `sync.WaitGroup` | Wait for goroutines to complete |
30| `context.Context` | Cancellation and deadlines |
31 
32### 2. Go Concurrency Mantra
33 
34```
35Don't communicate by sharing memory;
36share memory by communicating.
37```
38 
39## Quick Start
40 
41```go
42package main
43 
44import (
45 "context"
46 "fmt"
47 "sync"
48 "time"
49)
50 
51func main() {
52 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
53 defer cancel()
54 
55 results := make(chan string, 10)
56 var wg sync.WaitGroup
57 
58 // Spawn workers
59 for i := 0; i < 3; i++ {
60 wg.Add(1)
61 go worker(ctx, i, results, &wg)
62 }
63 
64 // Close results when done
65 go func() {
66 wg.Wait()
67 close(results)
68 }()
69 
70 // Collect results
71 for result := range results {
72 fmt.Println(result)
73 }
74}
75 
76func worker(ctx context.Context, id int, results chan<- string, wg *sync.WaitGroup) {
77 defer wg.Done()
78 
79 select {
80 case <-ctx.Done():
81 return
82 case results <- fmt.Sprintf("Worker %d done", id):
83 }
84}
85```
86 
87## Detailed patterns and worked examples
88 
89Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
90 
91## Best Practices
92 
93### Do's
94 
95- **Use context** - For cancellation and deadlines
96- **Close channels** - From sender side only
97- **Use errgroup** - For concurrent operations with errors
98- **Buffer channels** - When you know the count
99- **Prefer channels** - Over mutexes when possible
100 
101### Don'ts
102 
103- **Don't leak goroutines** - Always have exit path
104- **Don't close from receiver** - Causes panic
105- **Don't use shared memory** - Unless necessary
106- **Don't ignore context cancellation** - Check ctx.Done()
107- **Don't use time.Sleep for sync** - Use proper primitives
108 

Reviews

Installed this one?Write the first review and take the Trailblazer badge.

Reviews only open after a real install, so this is empty — and we leave it empty rather than invent one.

Alternatives

Also in Coding