golang-context

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

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers frequently break the context chain by creating context.Background() inside handlers, leaking cancel functions, storing context in structs, or losing trace IDs in background goroutines. This Skill provides the rules and patterns to propagate context.Context correctly across API boundaries, services, and databases. ## Core Features & Use Cases - Propagation Rules: Enforces passing ctx as the first parameter through the entire request lifecycle, from HTTP handler to service to database, using r.Context() and *Context database variants. - Cancellation & Timeouts: Covers WithCancel, WithTimeout, WithDeadline, nested deadline semantics, AfterFunc cleanup callbacks, and WithoutCancel for background work that must outlive the request. - Context Values & Tracing: Defines safe patterns for request-scoped values with unexported key types and trace ID propagation across HTTP service boundaries. - Use Case: When writing an HTTP handler that must send an audit log after responding, use context.WithoutCancel so the goroutine keeps the trace_id but survives client disconnection. ## Quick Start Review my Go HTTP handler and service layer for context propagation, cancellation, and timeout issues.

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 ctx through every downstream call. Use http.NewRequestWithContext for outbound HTTP and QueryContext or ExecContext for database calls so client disconnection cancels 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 migration.

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

Use context.WithoutCancel (Go 1.21+) to derive a context that keeps request-scoped values like trace_id but detaches cancellation. Pass it to the background goroutine so the work survives client disconnection without losing trace context.

Why does my Go context timeout leak resources?▼

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

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

No, storing context in a struct is an anti-pattern because it ties request lifetime to object lifetime and hides dependencies. Pass ctx explicitly as the first function parameter, named ctx context.Context, through the call chain.

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

Use an unexported custom type such as type contextKey string as the key to prevent collisions between packages. Reserve context values for request-scoped metadata like trace IDs and authenticated user info, never for function parameters or infrastructure dependencies.