go-context
Implement proper context management in Go for better control over execution.
Install
mkdir -p .claude/skills/go-context && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/16218" && unzip -o skill.zip -d .claude/skills/go-context && rm skill.zipInstalls to .claude/skills/go-context
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
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).Key capabilities
- →Pass `context.Context` as the first parameter in Go functions
- →Avoid storing `context.Context` in struct types
- →Avoid creating custom `context.Context` types
- →Store request-scoped data in `context.Context` values
- →Derive contexts with timeouts, cancellations, or deadlines
How it works
The skill provides guidelines and patterns for using `context.Context` in Go, emphasizing its role as the first parameter in functions, its immutability, and its use for cancellation signals and request-scoped data.
Inputs & outputs
When to use go-context
- →Add context to function signature
- →Propagate cancellation
- →Implement request timeout
- →Handle context values
About this skill
Go Context Usage
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
Read 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:
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
When not to use it
- →For goroutine lifecycle or sync primitives, which are covered by `go-concurrency`
- →For optional function parameters or data that could be passed explicitly
- →For configuration that does not vary per-request
Prerequisites
Limitations
- →Does not cover goroutine lifecycle or sync primitives
- →Does not cover error handling related to `ctx.Err()`
- →Does not cover interface design with context
How it compares
This skill provides specific Go conventions for `context.Context` usage, such as always passing it as the first parameter and avoiding its storage in structs, which differs from general Go programming practices.
Compared to similar skills
go-context side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| go-context (this skill) | 0 | 4mo | No flags | Beginner |
| golang-pro | 14 | 4mo | No flags | Advanced |
| go-concurrency-patterns | 7 | 2mo | No flags | Advanced |
| generating-grpc-services | 1 | 1mo | Review | Advanced |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by ComeOnOliver
View all by ComeOnOliver →You might also like
golang-pro
sickn33
Master Go 1.21+ with modern patterns, advanced concurrency, performance optimization, and production-ready microservices. Expert in the latest Go ecosystem including generics, workspaces, and cutting-edge frameworks. Use PROACTIVELY for Go development, architecture design, or performance optimization.
go-concurrency-patterns
wshobson
Master Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.
generating-grpc-services
jeremylongshore
Generate gRPC service definitions, stubs, and implementations from Protocol Buffers. Use when creating high-performance gRPC services. Trigger with phrases like "generate gRPC service", "create gRPC API", or "build gRPC server".
go-patterns
tuyenht
Các mẫu thiết kế Go: concurrency, interfaces, error handling và clean architecture.
architecture-patterns
wshobson
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use when architecting complex backend systems or refactoring existing applications for better maintainability.
workflow-orchestration-patterns
wshobson
Design durable workflows with Temporal for distributed systems. Covers workflow vs activity separation, saga patterns, state management, and determinism constraints. Use when building long-running processes, distributed transactions, or microservice orchestration.