go-context

Guides correct propagation, cancellation, and value handling of context.Context in Go code.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-context-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-context
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-context
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-context-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers frequently misuse context.Context by creating fresh Background contexts mid-request, leaking resources from un-cancelled derived contexts, or storing contexts in structs, which breaks cancellation chains and loses trace data. ## Core Features & Use Cases - Propagation Discipline: Enforces taking ctx as the first parameter and propagating the caller's context through the entire call chain instead of recreating it. - Cancellation & Timeouts: Covers WithCancel, WithTimeout, WithDeadline, AfterFunc, and WithoutCancel with mandatory deferred cancel patterns. - Request-Scoped Values & Tracing: Shows how to carry trace IDs and request metadata under unexported key types and propagate them across HTTP services. - Use Case: When reviewing a Go HTTP handler that calls a database and spawns goroutines, use this Skill to verify the request context reaches QueryContext calls and that background work detaches via context.WithoutCancel. ## Quick Start Review my Go HTTP handler and service layer to check that context is propagated correctly with proper timeouts and cancellation.

Frequently Asked Questions about go-context

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

FAQPage Schema
How do I use context.Context in Go HTTP handlers?

Use r.Context() inside handlers to get the request context, which is cancelled when the client disconnects. Pass it to all downstream calls using context-aware APIs like http.NewRequestWithContext and QueryContext, never create a new context.Background() mid-request.

When should I use context.WithTimeout vs WithDeadline in Go?

Use WithTimeout for a relative duration like 3 seconds from now, and WithDeadline for an absolute point in time such as an SLA timestamp. Always defer cancel() immediately after deriving, and note that nested timeouts take the shorter deadline.

Why does my Go context leak resources?

Leaks happen when cancel is never called after WithCancel, WithTimeout, or WithDeadline, leaving timers and goroutines allocated. Fix it by calling defer cancel() on the same line you derive the context, and verify with go vet.

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

No, storing context in a struct is an anti-pattern because it hides the cancellation lifetime and couples it to the struct's lifetime. Pass ctx explicitly as the first function parameter through the call chain instead.

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

Use context.WithoutCancel (Go 1.21+) to detach background work from the request's cancellation while preserving values like trace IDs. This avoids both killing the work when the handler returns and losing trace data with a fresh Background context.