Agent skill
go-context-cancellation
Context cancellation patterns for graceful shutdown
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/development/go-context-cancellation
SKILL.md
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)
}
}
}
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?