← ClaudeAtlas

golang-contextlisted

Idiomatic context.Context usage in Golang — creation, propagation, cancellation, timeouts, deadlines, context values, and cross-service tracing. Apply when working with context.Context in any Go code.
guynhsichngeodiec/cc-skills-golang · ★ 0 · AI & Automation · score 78
Install: claude install-skill guynhsichngeodiec/cc-skills-golang
> **Community default.** A company skill that explicitly supersedes `samber/cc-skills-golang@golang-context` skill takes precedence. # Go context.Context Best Practices `context.Context` is Go's mechanism for propagating cancellation signals, deadlines, and request-scoped values across API boundaries and between goroutines. Think of it as the "session" of a request — it ties together every operation that belongs to the same unit of work. ## Best Practices Summary 1. The same context MUST be propagated through the entire request lifecycle: HTTP handler → service → DB → external APIs 2. `ctx` MUST be the first parameter, named `ctx context.Context` 3. NEVER store context in a struct — pass explicitly through function parameters 4. NEVER pass `nil` context — use `context.TODO()` if unsure 5. `cancel()` MUST always be deferred immediately after `WithCancel`/`WithTimeout`/`WithDeadline` 6. `context.Background()` MUST only be used at the top level (main, init, tests) 7. **Use `context.TODO()`** as a placeholder when you know a context is needed but don't have one yet 8. NEVER create a new `context.Background()` in the middle of a request path 9. Context value keys MUST be unexported types to prevent collisions 10. Context values MUST only carry request-scoped metadata — NEVER function parameters 11. **Use `context.WithoutCancel`** (Go 1.21+) when spawning background work that must outlive the parent request ## Creating Contexts | Situation | Use | | --- | --- | | Entry point