golang-context

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

1|Updated Jun 2, 2026
One-click install
npx skills add https://github.com/VerifiedOrganic/onboard --skill golang-context-verifiedorganic
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/VerifiedOrganic/onboard/tree/main/.agents/skills/golang-context
Command: npx skills add https://github.com/VerifiedOrganic/onboard --skill golang-context-verifiedorganic

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers frequently misuse context.Context: breaking cancellation chains with context.Background() inside handlers, leaking resources by forgetting cancel(), storing contexts in structs, or colliding on string value keys. This Skill encodes the correct patterns so generated or reviewed Go code propagates cancellation, deadlines, and request-scoped values properly across API boundaries. ## Core Features & Use Cases - Propagation rules: Enforces passing ctx as the first parameter, using r.Context() in HTTP handlers, and never creating context.Background() mid-request. - Cancellation and timeouts: Covers WithCancel, WithTimeout, WithDeadline, nested deadline semantics, ctx.Done() select patterns, and always calling cancel() to prevent leaks. - Modern Go 1.21+ APIs: Teaches context.WithoutCancel for background work that must outlive a request (e.g., audit logging) and context.AfterFunc for non-blocking cleanup callbacks. - Use Case: When writing an HTTP handler that queries a database and calls an external payment API, the Skill ensures the request context flows through QueryRowContext and http.NewRequestWithContext so client disconnects cancel all downstream work. ## Quick Start Ask the agent to review or write Go code involving context usage, such as: review my HTTP handler and service layer for correct context propagation and timeout handling.

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 Go HTTP handlers?▼

Use r.Context() inside the handler to obtain the request context, then pass it as the first parameter through every downstream call. Use context-aware APIs like http.NewRequestWithContext and QueryRowContext so client disconnects cancel all downstream 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 migration.

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

Use context.WithoutCancel (Go 1.21+) to detach cancellation while preserving request-scoped values like trace_id. Passing r.Context() directly gets cancelled when the handler returns, while context.Background() loses trace values.

Why does my Go context timeout leak resources?▼

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

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

No, storing context in a struct is an anti-pattern because it blurs the context's lifetime and scope. Pass ctx explicitly as the first function parameter, named ctx context.Context, through the entire call chain.

What key type should I use for context values in Go?▼

Use an unexported custom type such as type contextKey string to prevent key collisions between packages. Plain string keys like "trace_id" can collide when multiple packages use the same key, silently overwriting each other's values.