
Go Context
Apply Go context.Context conventions—placement, cancellation, deadlines, and values—when writing or reviewing services and handlers.
Overview
Go-context is an agent skill for the Build phase that enforces idiomatic context.Context placement, cancellation, deadlines, and value usage in Go backends.
Install
npx skills add https://github.com/cxuu/golang-skills --skill go-contextWhat is this skill?
- Context as the first parameter convention with signature examples
- Explicit rule: do not store Context on structs; pass per method instead
- Guidance for propagating cancellation and deadlines on long-running operations
- Request-scoped values versus parameters—when each belongs
- Aligned with Go Wiki CodeReviewComments; Go 1.7+ standard library context
- Requires Go 1.7+ (context in standard library)
- Sourced from Go Wiki CodeReviewComments
Adoption & trust: 661 installs on skills.sh; 110 GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your Go handlers and services mishandle timeouts, hide context on structs, or pass request data in ways that break cancellation and review norms.
Who is it for?
Solo developers building Go HTTP services, gRPC-style APIs, or workers that must respect client disconnects and deadlines.
Skip if: Greenfield projects in non-Go languages or teams wanting goroutine pool patterns without context APIs—use go-concurrency instead for lifecycle primitives.
When should I use this skill?
When working with context.Context in Go—signatures, cancellation, deadlines, and values—or when the user cancels operations, sets timeouts, or passes request-scoped data without naming context explicitly.
What do I get? / Deliverables
Functions and methods follow first-parameter context flow with clear cancellation and scoped values suitable for production APIs.
- Refactored signatures with ctx first parameter
- Struct designs without embedded Context fields
- Timeout and cancellation wiring notes
Recommended Skills
Journey fit
Context discipline is enforced while implementing Go APIs and workers, which is core backend build work. Backend subphase covers request-scoped cancellation, timeouts, and RPC-style APIs where context must be first-parameter and never stored on structs.
How it compares
Idiomatic Go context reference skill—not a framework generator or infrastructure deploy workflow.
Common Questions / FAQ
Who is go-context for?
Indie and solo backend developers writing Go services who want agent-assisted reviews that match Go community standards for context.Context.
When should I use go-context?
During Build when adding handlers, refactoring service methods, setting timeouts on outbound calls, or reviewing PRs that pass request-scoped data.
Is go-context safe to install?
It is documentation-style guidance; review the Security Audits panel on this Prism page and the golang-skills repo before adding to your agent.
SKILL.md
READMESKILL.md - Go Context
# Go Context Usage ## Context as First Parameter Functions that use a Context should accept it as their **first parameter**: ```go func F(ctx context.Context, /* other arguments */) error func ProcessRequest(ctx context.Context, req *Request) (*Response, error) ``` This is a strong convention in Go that makes context flow visible and consistent across codebases. --- ## Don't Store Context in Structs Do not add a Context member to a struct type. Instead, pass `ctx` as a parameter to each method that needs it: ```go // Bad: Context stored in struct type Worker struct { ctx context.Context // Don't do this } // Good: Context passed to methods type Worker struct{ /* ... */ } func (w *Worker) Process(ctx context.Context) error { // Context explicitly passed — lifetime clear } ``` **Exception**: Methods whose signature must match an interface in the standard library or a third-party library may need to work around this. --- ## Don't Create Custom Context Types Do not create custom Context types or use interfaces other than `context.Context` in function signatures: ```go // Bad: Custom context type type MyContext interface { context.Context GetUserID() string } // Good: Use standard context.Context with value extraction func Process(ctx context.Context) error { userID := GetUserID(ctx) } ``` --- ## Where to Put Application Data Consider these options in order of preference: 1. **Function parameters** — most explicit and type-safe 2. **Receiver** — for data that belongs to the type 3. **Globals** — for truly global configuration (use sparingly) 4. **Context value** — only for request-scoped data Context values are appropriate for: - Request IDs and trace IDs - Authentication/authorization info that flows with requests - Deadlines and cancellation signals Context values are **not** appropriate for: - Optional function parameters - Data that could be passed explicitly - Configuration that doesn't vary per-request --- ## Common Patterns > Read [references/PATTERNS.md](references/PATTERNS.md) when deriving contexts (WithTimeout, WithCancel, WithDeadline), checking cancellation in loops or HTTP handlers, using context values with typed keys, or needing the quick reference table. ### Deriving Contexts Always `defer cancel()` immediately after creating a derived context: ```go ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() ``` ### Checking Cancellation ```go select { case <-ctx.Done(): return ctx.Err() default: // Do work } ``` ### Context Immutability Contexts are immutable — it's safe to pass the same `ctx` to multiple concurrent calls that share the same deadline and cancellation signal. --- ## Related Skills - **Goroutine coordination**: See [go-concurrency](../go-concurrency/SKILL.md) when using context for goroutine cancellation, select-based timeouts, or errgroup - **Error handling**: See [go-error-handling](../go-error-handling/SKILL.md) when deciding how to wrap or return `ctx.Err()` cancellation errors - **Interface design**: See [go-interfaces](../go-interfaces/SKILL.md) when designing APIs that accept context alongside interfaces - **Request-scoped logging**: See [go-logging](../go-logging/SKILL.md) when injecting loggers into context or adding request IDs to structured log output # Context Patterns Common patterns for deriving, checking, and propagating `context.Context`. --- #