
Go Context
Covers the conventions and patterns for using context.Context in Go, including the first-parameter rule, why you shouldn't store contexts in structs, and when to use context values versus explicit parameters. Includes practical examples for deriving contexts with timeouts and cancellation, plus guidance on what belongs in context (request IDs, auth tokens) versus what doesn't (optional parameters, configuration). The priority ordering for application data is genuinely helpful: function parameters first, receiver second, globals sparingly, context values last. If you're working on HTTP handlers, background jobs, or anything that needs cancellation propagation in Go, this gets you following the community conventions without having to rediscover them through code review.
- skills.sh installs
- 1.3K
- GitHub stars
- 193
- last updated
- Aug 21, 2026
Install
$ npx -y skills add cxuu/golang-skills --skill go-context --agent claude-codeInstalls into .claude/skills of the current project.
Files
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.
Sponsors
Details
- Repository
- github.com/cxuu/golang-skills
- Category
- Go
- First seen
- May 16, 2026
More from cxuu/golang-skills
All 20- Go Naming1.3K
- Go Defensive1.3K
- Go Packages1.3K
- Go Data Structures1.3K
- Go Functional Options1.3K
- Go Control Flow1.3K
- Go Logging1.1K
- Go Functions1.1K
- Go Generics1.1K
- Go Declarations1.1K
- Go Code Review1.8K
- Go Testing1.4K
- Go Error Handling1.3K
- Go Linting1.3K
- Go Concurrency1.3K
- Go Interfaces1.3K
- Go Documentation1.3K
- Go Style Core1.3K
- Go Performance1.3K