Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
samber avatar

Golang Context

  • 34.2k installs
  • 2.8k repo stars
  • Updated July 27, 2026
  • samber/cc-skills-golang

golang-context is an agent skill for idiomatic context.Context propagation, cancellation, timeouts, values, and WithoutCancel in Go.

About

The golang-context skill v1.2.1 teaches idiomatic context.Context usage as the request session tying together handler, service, database, and external API work. Eleven best-practice rules require ctx as the first parameter, prohibit storing context in structs, mandate cancel on all WithCancel paths, restrict context.Background to top-level entry points, and limit values to request-scoped metadata with unexported key types. Creating contexts table maps Background, TODO, r.Context, WithCancel, and WithTimeout to appropriate situations. Propagation examples contrast breaking the chain with context.Background inside services versus passing caller ctx to db.ExecContext. Deep-dive references cover cancellation and WithoutCancel for audit logs that outlive requests, safe value keys for tracing, and HTTP client plus QueryContext database patterns. Cross-references link to golang-concurrency, golang-database, golang-observability, and golang-design-patterns skills. Linters like govet and staticcheck catch many context pitfalls automatically during review.

  • Eleven must-follow rules for ctx first parameter, no struct storage, and cancel on all control paths.
  • Propagation principle: same context flows handler to service to database to external APIs.
  • Documents WithCancel, WithTimeout, WithDeadline, and Go 1.21+ WithoutCancel for outliving work.
  • Context values limited to request metadata with unexported keys, never function parameters.
  • HTTP and database sections require r.Context, NewRequestWithContext, and QueryContext variants.

Golang Context by the numbers

  • 34,151 all-time installs (skills.sh)
  • +535 installs in the week ending Jul 28, 2026 (Skillselion tracking)
  • Ranked #5 of 99 Go skills by installs in the Skillselion catalog
  • Security screen: LOW risk (skills.sh audit)
  • Data as of Jul 28, 2026 (Skillselion catalog sync)
At a glance

golang-context capabilities & compatibility

Capabilities
context propagation across layers · cancellation and timeout patterns · withoutcancel for outliving requests · safe context value key design · http and database context integration
Use cases
api development · debugging
From the docs

What golang-context says it does

The same context MUST be propagated through the entire request lifecycle
retag-ops/docs-cache/skill_samber_cc-skills-golang_golang-context.md
npx skills add https://github.com/samber/cc-skills-golang --skill golang-context

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs34.2k
repo stars2.8k
Security audit3 / 3 scanners passed
Last updatedJuly 27, 2026
Repositorysamber/cc-skills-golang

How do I propagate context correctly so cancellation, deadlines, and tracing work across Go service layers?

Propagate context.Context for cancellation, timeouts, deadlines, request-scoped values, and WithoutCancel background work in Go.

Who is it for?

Go developers designing handlers, services, HTTP clients, or database calls that must respect cancellation.

Skip if: Skip when code merely accepts ctx without propagation, cancellation, or value design decisions.

When should I use this skill?

Debugging leaked contexts, choosing Background versus TODO, or storing trace IDs across API boundaries.

What you get

Consistent context chains with proper cancel calls, timeout boundaries, and safe request-scoped values.

  • Correct context propagation
  • Timeout and cancel patterns

By the numbers

  • Metadata version 1.2.1 with eleven numbered best-practice rules.
  • Creating contexts table covers five common situations.

Files

SKILL.mdMarkdownGitHub ↗
Community default. A company skill that explicitly supersedes samber/cc-skills-golang@golang-context skill takes precedence.

Go context.Context Best Practices

context.Context is Go's mechanism for propagating cancellation signals, deadlines, and request-scoped values across API boundaries and between goroutines. Think of it as the "session" of a request — it ties together every operation that belongs to the same unit of work.

Best Practices Summary

1. The same context MUST be propagated through the entire request lifecycle: HTTP handler → service → DB → external APIs 2. ctx MUST be the first parameter, named ctx context.Context 3. NEVER store context in a struct — pass explicitly through function parameters 4. NEVER pass nil context — use context.TODO() if unsure 5. cancel() MUST be called on all control-flow paths for WithCancel/WithTimeout/WithDeadline, unless ownership of the context and cancel function is explicitly returned or transferred 6. context.Background() MUST only be used at the top level (main, init, tests) 7. Use `context.TODO()` as a placeholder when you know a context is needed but don't have one yet 8. NEVER create a new context.Background() in the middle of a request path 9. Context value keys MUST be unexported types to prevent collisions 10. Context values MUST only carry request-scoped metadata — NEVER function parameters 11. Use `context.WithoutCancel` (Go 1.21+) when spawning background work that must outlive the parent request

Creating Contexts

SituationUse
Entry point (main, init, test)context.Background()
Function needs context but caller doesn't provide one yetcontext.TODO()
Inside an HTTP handlerr.Context()
Need cancellation controlcontext.WithCancel(parentCtx)
Need a deadline/timeoutcontext.WithTimeout(parentCtx, duration)

Context Propagation: The Core Principle

The most important rule: propagate the same context through the entire call chain. When you propagate correctly, cancelling the parent context cancels all downstream work automatically.

// ✗ Bad — creates a new context, breaking the chain
func (s *OrderService) Create(ctx context.Context, order Order) error {
    return s.db.ExecContext(context.Background(), "INSERT INTO orders ...", order.ID)
}

// ✓ Good — propagates the caller's context
func (s *OrderService) Create(ctx context.Context, order Order) error {
    return s.db.ExecContext(ctx, "INSERT INTO orders ...", order.ID)
}

Deep Dives

  • [Cancellation, Timeouts & Deadlines](./references/cancellation.md) — How cancellation propagates: WithCancel for manual cancellation, WithTimeout for automatic cancellation after a duration, WithDeadline for absolute time deadlines. Patterns for listening (<-ctx.Done()) in concurrent code, AfterFunc callbacks, and WithoutCancel for operations that must outlive their parent request (e.g., audit logs).
  • [Context Values & Cross-Service Tracing](./references/values-tracing.md) — Safe context value patterns: unexported key types to prevent namespace collisions, when to use context values (request ID, user ID) vs function parameters. Trace context propagation: OpenTelemetry trace headers, correlation IDs for log aggregation, and marshaling/unmarshaling context across service boundaries.
  • [Context in HTTP Servers & Service Calls](./references/http-services.md) — HTTP handler context: r.Context() for request-scoped cancellation, middleware integration, and propagating to services. HTTP client patterns: NewRequestWithContext, client timeouts, and retries with context awareness. Database operations: always use *Context variants (QueryContext, ExecContext) to respect deadlines.

Cross-References

  • → See the samber/cc-skills-golang@golang-concurrency skill for goroutine cancellation patterns using context
  • → See the samber/cc-skills-golang@golang-database skill for context-aware database operations (QueryContext, ExecContext)
  • → See the samber/cc-skills-golang@golang-observability skill for trace context propagation with OpenTelemetry
  • → See the samber/cc-skills-golang@golang-design-patterns skill for timeout and resilience patterns

Enforce with Linters

Many context pitfalls are caught automatically by linters: govet, staticcheck. → See the samber/cc-skills-golang@golang-lint skill for configuration and usage.

Related skills

How it compares

Go context propagation guide, not a generic concurrency primer.

FAQ

Who is golang-context for?

Go developers implementing cancellation, timeouts, and request-scoped values across service layers.

When should I use golang-context?

When fixing broken context chains, adding deadlines, or using WithoutCancel for background audit work.

Is golang-context safe to install?

Review the Security Audits panel on this page before installing in production.

Gobackend

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.