golang-context

Guides idiomatic context.Context propagation, cancellation, timeouts, and request-scoped values in Go code.

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-context-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-context
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-context-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers frequently misuse context.Context by breaking propagation chains, leaking cancel functions, storing contexts in structs, or abusing context values, leading to leaked goroutines, ignored cancellations, and lost trace data. ## Core Features & Use Cases - Propagation Rules: Enforces passing the same context through HTTP handlers, services, databases, and external API calls using r.Context() and *Context variants. - Cancellation & Timeouts: Covers WithCancel, WithTimeout, WithDeadline, nested deadline semantics, AfterFunc, and WithoutCancel for background work outliving requests. - Context Values & Tracing: Teaches unexported key types, what belongs in context values versus function parameters, and trace ID propagation across services. - Use Case: When writing an HTTP handler that must send an audit log after the client disconnects, use context.WithoutCancel to preserve the trace_id while detaching cancellation. ## Quick Start Ask the AI to review your Go HTTP handler or service method for correct context.Context propagation, timeout handling, and cancellation leaks.

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 services, database calls via QueryContext or ExecContext, and outbound requests via http.NewRequestWithContext. Never create context.Background() inside a handler.

When should I use context.WithoutCancel in Go?

Use context.WithoutCancel (Go 1.21+) when spawning background work that must outlive the parent request, such as audit logging after a handler returns. It preserves context values like trace_id while detaching cancellation, unlike context.Background() which loses values.

What is the difference between context.Background and context.TODO?

context.Background() is for true top-level entry points like main, init, and tests. context.TODO() is a placeholder when a function needs a context but the caller does not provide one yet, typically during incremental migrations.

Why does my Go context timeout leak resources?

Leaks happen when cancel() from WithCancel, WithTimeout, or WithDeadline is never called, leaving timers and cancellation state allocated. Defer cancel() immediately after creating the derived context on all control-flow paths.

Can I store context.Context in a struct field?

No, storing context in a struct is an anti-pattern. Pass ctx explicitly as the first function parameter named ctx context.Context so cancellation and deadlines flow through the call chain.

How do nested context timeouts behave in Go?

The shorter deadline always wins. If a parent context has a 2-second timeout and you create a child with 10 seconds, the child still expires at 2 seconds, so child timeouts cannot extend beyond the parent's remaining time.