← Back to list

go-context-cancellation
by JamesPrial
WIP - collection of various Claude stuff i make/use/have_Claude_hallucinate
⭐ 2🍴 0📅 Jan 23, 2026
SKILL.md
name: go-context-cancellation description: Context cancellation patterns for graceful shutdown
Context Cancellation
Pattern
Pass context.Context as first parameter. Check ctx.Done() in goroutines and long operations.
CORRECT
func ProcessItems(ctx context.Context, items []string) error {
for _, item := range items {
select {
case <-ctx.Done():
return ctx.Err()
default:
if err := processItem(item); err != nil {
return err
}
}
}
return nil
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := ProcessItems(ctx, items); err != nil {
log.Printf("processing failed: %v", err)
}
}
WRONG - No context checking
func ProcessItems(items []string) error {
for _, item := range items {
// No way to cancel - runs forever if stuck
if err := processItem(item); err != nil {
return err
}
}
return nil
}
Rules
- Context is first parameter:
func Do(ctx context.Context, ...) - Always call
cancel()viadeferto prevent leaks - Check
ctx.Done()in loops and before expensive operations - Propagate context to downstream calls
- Use
context.WithTimeoutorWithDeadlinefor time limits
Common Patterns
// HTTP server with context
func handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() // Request context
result, err := fetchData(ctx)
// ...
}
// Worker with cancellation
func worker(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case work := <-workCh:
process(work)
}
}
}
Score
Total Score
65/100
Based on repository quality metrics
✓SKILL.md
SKILL.mdファイルが含まれている
+20
✓LICENSE
ライセンスが設定されている
+10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
✓最近の活動
1ヶ月以内に更新
+10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
✓タグ
1つ以上のタグが設定されている
+5
Reviews
💬
Reviews coming soon
