golang-context

Guide Go developers to use context.Context for cancellation, deadlines, and request-scoped values.

5|1|Updated Apr 14, 2026
One-click install
npx skills add https://github.com/omarluq/og-template --skill golang-context-omarluq
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/omarluq/og-template/tree/main/.agents/skills/golang-context
Command: npx skills add https://github.com/omarluq/og-template --skill golang-context-omarluq

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go code bases often mishandle context.Context, breaking cancellation, timeouts, and trace propagation. This Skill provides idiomatic patterns for creation, propagation, cancellation, timeouts, deadlines, context values, and cross-service tracing across HTTP servers, services, and databases.

Core Features & Use Cases

  • Propagate the same context across HTTP handlers, service calls, and database queries to ensure graceful cancellation.
  • Use context values safely with unexported keys for tracing, request IDs, and tenant information across service boundaries.
  • Apply WithCancel, WithTimeout, WithDeadline, and WithoutCancel patterns to model long-running work and background tasks.

Quick Start

Start by wiring r.Context() in HTTP handlers, propagate the same context to downstream services and database calls with the *Context variants, and apply cancellation and timeouts as shown in the references.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I propagate context across HTTP handlers and database calls in Go?

To propagate context in Go, wire r.Context() from HTTP handlers and pass the same context.Context to downstream service calls and database queries using their *Context variants to ensure graceful cancellation and tracing.

What is the safest way to use context values for request IDs in distributed systems?

The safest way to use context values for request IDs in distributed systems is by storing them with unexported keys, preventing collisions and ensuring safe tracing across service boundaries.

How do I set timeouts and cancellation for long-running tasks in Go?

To set timeouts and cancellation for long-running tasks in Go, apply context.WithCancel, context.WithTimeout, or context.WithDeadline to model work boundaries and trigger graceful shutdowns when limits are exceeded.

When should I use WithoutCancel for background tasks in Go?

You should use context.WithoutCancel in Go when spawning background tasks that must continue running independently of the parent request's cancellation signal, detaching the lifecycle from the incoming HTTP request.

Why does my Go HTTP service break cancellation when calling downstream services?

Cancellation breaks in Go HTTP services when context.Context is not propagated correctly, requiring you to pass the same context to downstream service and database calls using their *Context variants to maintain the cancellation chain.

Does this Skill cover context usage for internal libraries and distributed systems?

Yes, this Skill covers context usage for internal libraries and distributed systems, applying idiomatic patterns for cancellation, deadlines, and request-scoped values across HTTP servers and databases.