golang-context

Propagate context.Context cancellation, deadlines, and values across Go services.

Updated Apr 24, 2026
One-click install
npx skills add https://github.com/Utchash007/TermTales --skill golang-context-utchash007
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/Utchash007/TermTales/tree/main/.agents/skills/golang-context
Command: npx skills add https://github.com/Utchash007/TermTales --skill golang-context-utchash007

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go projects often struggle with propagating cancellation, deadlines, and request-scoped values across HTTP handlers, services, and data stores, leading to leaked resources and brittle code.

Core Features & Use Cases

  • Propagate the same context through HTTP handlers, service layers, and DB calls to ensure cancellation and deadlines apply end-to-end.
  • Use the proper *Context wrappers and context-aware APIs (QueryContext, ExecContext, etc.) for database/query/HTTP operations to respect timeouts.
  • Provide guidance for best practices in HTTP servers, clients, and tracing to maintain consistent request-scoped data and clean shutdowns.

Quick Start

Refactor a Go service to propagate a single request context through HTTP calls, database queries, and downstream services using context.Context and the *Context variants.

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 signals and deadlines across Go HTTP handlers and database calls?

Propagate cancellation signals and deadlines across Go HTTP handlers and database calls by passing a single context.Context through service layers. Use r.Context() in HTTP handlers and context-aware APIs like QueryContext to ensure end-to-end cancellation.

Why should I avoid storing context.Context in a Go struct?

Avoid storing context.Context in a Go struct because contexts are designed for request-scoped flow, not long-lived storage. Passing contexts explicitly through function arguments ensures cancellation signals and deadlines propagate cleanly without leaking resources.

What is the best way to handle HTTP client timeouts and client disconnects in a Go service?

Handle HTTP client timeouts and disconnects in a Go service by propagating context.Context from the incoming request using r.Context(). This ensures downstream operations, including database queries and outgoing HTTP calls, abort immediately when the client disconnects or the deadline expires.

How do I pass request-scoped values through a Go service without using exported context keys?

Pass request-scoped values through a Go service using context.WithValue with unexported keys. This prevents external packages from accessing or overwriting your internal context values, maintaining type safety and clean request-scoped data propagation.

How do I use context for database queries in Go to prevent leaked resources?

Use context for database queries in Go by calling context-aware variants like QueryContext and ExecContext. Passing the request context ensures database operations respect timeouts and cancel cleanly on client disconnects, preventing leaked resources.

Does Go context.Context work with both HTTP servers and downstream service calls?

Go context.Context works seamlessly with HTTP servers and downstream service calls. By propagating a single context from r.Context() through HTTP clients, service layers, and database calls, you maintain consistent request-scoped data and clean shutdowns end-to-end.