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-code

Installs into .claude/skills of the current project.

Files

Go Context Usage

Compatibility: context has 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

Recommended

More Go
1samber avatargolang-clisamber/cc-skills-golangGolang CLI application development. Use when building, modifying, or reviewing a Go CLI tool — especially for command structure, flag handling, configuration la41.1Kskills.sh installs
2samber avatargolang-samber-slogsamber/cc-skills-golangStructured logging extensions for Golang using samber/slog-**** packages — multi-handler pipelines (slog-multi), log sampling (slog-sampling), attribute formatt39.8Kskills.sh installs
3samber avatargolang-swaggersamber/cc-skills-golangGolang OpenAPI/Swagger documentation with swaggo/swag — annotation comments (@Summary, @Param, @Success, @Router, @Security), swag init code generation, framewo38.6Kskills.sh installs
4getsentry avatarsentry-go-sdkgetsentry/sentry-for-aiFull Sentry SDK setup for Go. Use when asked to "add Sentry to Go", "install sentry-go", "setup Sentry in Go", or configure error monitoring, tracing, logging, 2.3Kskills.sh installs
5cxuu avatargo-concurrencycxuu/golang-skillsUse when writing concurrent Go code — goroutines, channels, mutexes, or thread-safety guarantees. Also use when parallelizing work, fixing data races, or protec1.3Kskills.sh installs
6cxuu avatargo-performancecxuu/golang-skillsUse when optimizing Go code, investigating slow performance, or writing performance-critical sections. Also use when a user mentions slow Go code, string concat1.3Kskills.sh installs