golang-context
Idiomatic context.Context usage in Golang — propagation through API boundaries, cancellation, timeouts and deadlines, request-scoped values, context.WithoutCancel for background work outliving requests. Apply when designing context propagation across layers, debugging leaked or unexpired contexts, choosing between context.Background/TODO/WithoutCancel, or storing values in context. Not for code that merely accepts ctx as first parameter.
Security Assessment
About golang-context
The golang-context skill provides developers with best practices and guidance for idiomatic usage of Go's context.Context. It helps ensure proper propagation of cancellation signals, deadlines, and request-scoped values across API boundaries and between goroutines. By following these patterns, developers can avoid common pitfalls such as leaked contexts, unexpired operations, or misuse of context values. This skill is essential for maintaining clean, maintainable, and robust Go applications that handle concurrent operations safely.
The skill covers key features including proper context propagation through function call chains, using context.Background and context.TODO appropriately, creating contexts with cancellation, timeouts, or deadlines, and storing request-scoped values safely. It also introduces advanced capabilities like context.WithoutCancel for background tasks that must outlive a parent request. The documentation emphasizes consistent parameter naming, avoiding storing contexts in structs, and ensuring cancellation functions are called to prevent resource leaks.
Target users include Go developers building services or applications that rely on concurrent operations, HTTP handlers, database interactions, or cross-service communication. It's particularly useful for engineers debugging context leaks, designing context-aware APIs, or ensuring proper lifecycle management of requests. By following these practices, developers can implement reliable, traceable, and efficient context management across their Go projects.
FAQ
When should I use context.Background() versus context.TODO()?
Use context.Background() at the top level of applications (main, init, tests) as a root context. Use context.TODO() as a placeholder when a context is required but not yet available or defined.
Is golang-context compatible with all Go projects?
The skill is designed for projects using Golang and AI coding agents like Claude Code. It follows idiomatic Go patterns, so compatibility is broad for Go services and libraries.
Can I store a context in a struct for reuse?
No, contexts should never be stored in structs. They must be passed explicitly through function parameters to maintain proper propagation and cancellation behavior.
What should I do if I need a background task that outlives a request?
Use context.WithoutCancel (available in Go 1.21+) to spawn background work that must continue after the parent request has completed.
What are the main requirements for cancelling contexts?
For contexts created with WithCancel, WithTimeout, or WithDeadline, the cancel function must be called on all control-flow paths unless ownership is explicitly returned or transferred.
Install golang-context
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
samber/cc-skills-golang