How to install go-context
npx skills add https://github.com/cxuu/golang-skills --skill go-contextFull instructions (SKILL.md)
Source of truth, from cxuu/golang-skills.
name: go-context description: Use when working with context.Context in Go — placement in signatures, propagating cancellation and deadlines, and storing values in context vs parameters. Also use when cancelling long-running operations, setting timeouts, or passing request-scoped data, even if they don't mention context.Context directly. Does not cover goroutine lifecycle or sync primitives (see go-concurrency).
Go Context Usage
Compatibility:
contexthas been in the standard library since Go 1.7.
Resource Routing
references/PATTERNS.md- Read when deriving contexts, checking cancellation, handling HTTP request contexts, or using typed context-value keys.
Context as First Parameter
Functions that use a Context should accept it as their first parameter:
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:
// 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:
// 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:
- Function parameters — most explicit and type-safe
- Receiver — for data that belongs to the type
- Globals — for truly global configuration (use sparingly)
- 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
Deriving Contexts
Always defer cancel() immediately after creating a derived context:
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
Checking Cancellation
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 when using context for goroutine cancellation, select-based timeouts, or errgroup
- Error handling: See go-error-handling when deciding how to wrap or return
ctx.Err()cancellation errors - Interface design: See go-interfaces when designing APIs that accept context alongside interfaces
- Request-scoped logging: See go-logging when injecting loggers into context or adding request IDs to structured log output
Related skills
More from cxuu/golang-skills and the wider catalog.
go-code-review
Use when reviewing Go code or checking code against community style standards. Also use proactively before submitting a Go PR or when reviewing any Go code changes, even if the user doesn't explicitly request a style review. Does not cover language-specific syntax — delegates to specialized skills.
go-testing
Use when writing, reviewing, or improving Go test code — including table-driven tests, subtests, parallel tests, test helpers, test doubles, and assertions with cmp.Diff. Also use when a user asks to write a test for a Go function, even if they don't mention specific patterns like table-driven tests or subtests. Does not cover benchmark performance testing (see go-performance).
go-linting
Use when setting up linting for a Go project, configuring golangci-lint, or adding Go checks to a CI/CD pipeline. Also use when starting a new Go project and deciding which linters to enable, even if the user only asks about "code quality" or "static analysis" without mentioning specific linter names. Does not cover code review process (see go-code-review).
go-documentation
Use when writing or reviewing documentation for Go packages, types, functions, or methods. Also use proactively when creating new exported types, functions, or packages, even if the user doesn't explicitly ask about documentation. Does not cover code comments for non-exported symbols (see go-style-core).
go-performance
Use when optimizing Go code, investigating slow performance, or writing performance-critical sections. Also use when a user mentions slow Go code, string concatenation in loops, or asks about benchmarking, even if the user doesn't explicitly mention performance patterns. Does not cover concurrent performance patterns (see go-concurrency).
go-error-handling
Use when writing Go code that returns, wraps, or handles errors — choosing between sentinel errors, custom types, and fmt.Errorf (%w vs %v), structuring error flow, or deciding whether to log or return. Also use when propagating errors across package boundaries or using errors.Is/As, even if the user doesn't ask about error strategy. Does not cover panic/recover patterns (see go-defensive).