golang-context

Guides idiomatic context.Context propagation, cancellation, and timeout patterns in Go code.

Updated May 9, 2026
One-click install
npx skills add https://github.com/LuminaVault/LuminaVaultShared --skill golang-context-luminavault
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/LuminaVault/LuminaVaultShared/tree/main/.agents/skills/golang-context
Command: npx skills add https://github.com/LuminaVault/LuminaVaultShared --skill golang-context-luminavault

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers frequently misuse context.Context by breaking the propagation chain, leaking cancel functions, storing contexts in structs, or losing trace values in background work. This Skill provides the rules and patterns to write correct, idiomatic context handling across HTTP handlers, services, and databases. ## Core Features & Use Cases - Propagation Rules: Enforces passing the same context through the entire request lifecycle, with ctx as the first parameter and no contexts stored in structs. - Cancellation & Timeouts: Covers WithCancel, WithTimeout, WithDeadline, nested deadline semantics, AfterFunc cleanup callbacks, and WithoutCancel for background work that outlives requests. - Context Values & Tracing: Teaches unexported key types, what belongs in context values versus function parameters, and trace ID propagation across HTTP services. - Use Case: When writing an HTTP handler that calls a database and an external payment API, apply this Skill to propagate r.Context() through QueryRowContext and http.NewRequestWithContext so client disconnects cancel all downstream work. ## Quick Start Review my Go HTTP handler and service layer for correct context.Context propagation, cancellation, and timeout usage.

Frequently Asked Questions about golang-context

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I propagate context.Context through a Go HTTP handler?▼

Use r.Context() inside the handler to obtain the request context, then pass it as the first parameter to every downstream call. Use context-aware APIs like QueryRowContext for databases and http.NewRequestWithContext for outgoing HTTP requests so client disconnects cancel all work.

When should I use context.Background vs context.TODO in Go?▼

Use context.Background() only at true top-level entry points like main, init, or tests. Use context.TODO() as a temporary placeholder when a function needs a context but its caller does not provide one yet, such as during incremental migrations.

How do I run background work after a Go HTTP request completes?▼

Use context.WithoutCancel (Go 1.21+) to create a child context detached from the parent's cancellation while preserving values like trace IDs. Pass it to the background goroutine instead of r.Context(), which is cancelled when the handler returns, or context.Background(), which loses request values.

Why does my Go context timeout leak resources?▼

Every WithCancel, WithTimeout, and WithDeadline allocates cancellation state and timers that persist until cancel() is called. Always defer cancel() immediately after creating the derived context on all control-flow paths to release these resources.

Can I store context.Context in a Go struct field?▼

No, storing context in a struct is an anti-pattern because contexts are request-scoped while structs often outlive individual requests. Pass ctx explicitly as the first function parameter instead, named ctx context.Context.

What keys are safe for context values in Go?▼

Use unexported custom types as context keys, such as type contextKey string, to prevent collisions between packages. Plain string keys risk collisions when multiple packages use the same key name, silently overwriting each other's values.