golang-context

Propagate context.Context cancellation and deadlines across Go HTTP handlers and data layers.

2.9k|191|Updated Mar 21, 2026
One-click install
npx skills add https://github.com/samber/cc-skills-golang --skill golang-context
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/samber/cc-skills-golang/tree/main/skills/golang-context
Command: npx skills add https://github.com/samber/cc-skills-golang --skill golang-context

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go applications often struggle to propagate cancellation, deadlines, and request-scoped values across layers. This skill codifies idiomatic practices for using context.Context in Go, including propagation through HTTP handlers, services, and data stores, and avoiding common pitfalls like storing contexts in structs or bypassing context-aware APIs.

Core Features & Use Cases

  • Propagate cancellation and deadlines across HTTP, gRPC, and background tasks using WithCancel, WithTimeout, and WithDeadline.
  • Use context values judiciously for trace IDs and request-scoped metadata, without abusing context for business parameters.
  • Ensure all downstream calls (HTTP, database) use context-aware APIs (e.g., Do with Request with Context, QueryContext/ExecContext/QueryRowContext).

Quick Start

Refactor a Go service to consistently pass and propagate a context.Context through handlers, services, and database calls, avoiding storing Context in structs.

Frequently Asked Questions about golang-context

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I propagate cancellation and timeouts across Go HTTP handlers and database calls?

You propagate context.Context by passing it explicitly as the first parameter through HTTP handlers, services, and repository layers, using WithTimeout or WithCancel to ensure operations respect cancellation and deadlines across service boundaries.

What is the best way to use context with database queries in Go?

The best way to use context with database queries is to consistently call the context-aware variants QueryContext, ExecContext, and QueryRowContext rather than their non-context counterparts, ensuring queries are cancellable and respect request timeouts.

Why does storing context in a Go struct cause problems?

Storing context in a Go struct causes problems because it decouples the context's lifecycle from the request scope, leading to missed cancellations, leaked deadlines, and unpredictable behavior when the struct outlives the originating request.

Can I use context values for passing business parameters in Go?

You should not use context values for business parameters. Context values should be reserved for request-scoped metadata like trace IDs, avoiding abuse that obscures function signatures and makes dependencies difficult to trace and test.

When should I use WithCancel versus WithDeadline in Go context?

Use WithCancel when you need to manually trigger cancellation based on client disconnects or interrupts, and use WithDeadline or WithTimeout when operations must automatically cancel after a specific time or duration has elapsed.

How do I ensure downstream HTTP calls in Go respect context cancellation?

To ensure downstream HTTP calls respect cancellation, create an outbound request with http.NewRequestWithContext, then execute it using the Do method so the call terminates immediately when the parent context is cancelled or times out.