Go Concurrency Patterns

Go Concurrency Patterns

Teaches an AI coding agent production Go concurrency patterns: goroutines, channels, sync primitives, context, worker pools and graceful shutdown, plus race debugging.

Skill files2 files

references/

SKILL.md108 lines
1
---
2
name: go-concurrency-patterns
3
description: Master Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.
4
---
5
6
# Go Concurrency Patterns
7
8
Production 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
```
35
Don't communicate by sharing memory;
36
share memory by communicating.
37
```
38
39
## Quick Start
40
41
```go
42
package main
43
44
import (
45
"context"
46
"fmt"
47
"sync"
48
"time"
49
)
50
51
func 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
76
func 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
89
Detailed 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

© Seth Hobson · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 11,966 prompts

More by @OpenSourceBuffGuy

See all
Azure Static Web Apps prompt image

Azure Static Web Apps

Helps an agent create, configure and deploy Azure Static Web Apps with the SWA CLI, including local emulation, Functions APIs and CI/CD.

agent-skill, azure, deployment, github-actions, ci-cd, serverless, command-line, frontend, step-by-step-guide, for-developers, best-practices, authentication, web-development, scaffolding, cloud, api, nodejs

Workflow Automation Specialist prompt image

Workflow Automation Specialist

Finds repetitive tasks to automate, estimates time savings and ROI, and designs no-code workflows with Zapier, Make or native tools within your budget.

system-prompt, expert-advice, workflow-automation, no-code, productivity, zapier, make-com, for-non-coders, for-small-business-owners, beginner-friendly, xml-tags, estimation, planner, guardrails, structured-output, process-improvement, step-by-step-guide, consulting, time-management, office-administration

Giant Soda Can Frozen in Glacier prompt image

Giant Soda Can Frozen in Glacier

Draws a monumental soda can frozen in blue glacial ice in an arctic landscape, with a snowcat and tiny scientists for scale, as a cinematic low-angle advertising photo.

image-prompt, photorealistic, cinematic, low-angle, wide-shot, product-photography, advertising, winter, landscape, surreal, forced-perspective, vertical-format, soft-lighting, natural-light, muted-colors, prompt-weights, nano-banana

Reviews

Loading reviews…

Discussion

Loading comments…