golang-context

Document idiomatic context.Context usage for Go microservice backends.

Updated Mar 29, 2026
One-click install
npx skills add https://github.com/osmanozen/go-commerce --skill golang-context-osmanozen
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/osmanozen/go-commerce/tree/main/.agents/skills/golang-context
Command: npx skills add https://github.com/osmanozen/go-commerce --skill golang-context-osmanozen

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go developers often struggle with correctly creating, propagating, and canceling contexts across services. This Skill provides clear patterns and practices for using context.Context in Go, covering cancellation, timeouts, deadlines, values, and cross-service tracing.

Core Features & Use Cases

  • Context creation & propagation: proper initialization, passing ctx through call chains, and avoiding nil contexts.
  • Cancellation & deadlines: using WithCancel, WithTimeout, WithDeadline, and handling ctx.Done().
  • Context values & tracing: safe key types and extracting trace/user IDs across services.
  • HTTP and database integration: use of context-aware APIs like http.NewRequestWithContext and QueryContext/ExecContext.
  • Use Case: building a resilient, cancellable HTTP handler that calls a downstream service while respecting client disconnects.

Quick Start

Instantiate a request-scoped context from r.Context(), propagate it to HTTP client calls and database queries, and cancel it when the client disconnects.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I propagate Go context through HTTP and database calls?

To propagate Go context, instantiate a request-scoped context from r.Context() and pass it to context-aware APIs like http.NewRequestWithContext and QueryContext to ensure cancellations and deadlines are respected across call chains.

What's the best way to handle context cancellation and timeouts in Golang?

The best way to handle context cancellation and timeouts in Golang is using context.WithCancel, context.WithTimeout, or context.WithDeadline, and monitoring ctx.Done() to gracefully exit operations when the context expires.

How do I safely pass trace IDs using Go context values?

To safely pass trace IDs using Go context values, use custom, unexported key types rather than plain strings to store and retrieve request-scoped data like user or trace IDs across services without collisions.

How do I build a cancellable HTTP handler in Go that respects client disconnects?

To build a cancellable HTTP handler in Go, derive the context from r.Context() and propagate it to downstream HTTP or database calls, ensuring operations terminate immediately when the client disconnects.

When should I use WithDeadline vs WithTimeout in Go context?

Use context.WithTimeout when you need an operation to cancel after a specific duration, and use context.WithDeadline when you need cancellation at an absolute future time, both triggering ctx.Done() when expired.

Why should I avoid passing nil context in Go functions?

Passing a nil context in Go functions is unsafe and causes panics when the function attempts to derive a new context or check for cancellation, so always pass context.Background() or a request-scoped context.