golang-context

Propagate context.Context through Go HTTP servers, services, and repositories.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Many Go programs misuse context propagation, leading to cancelled work not being observed, resource leaks from forgotten cancel calls, lost tracing information, and background work that is either prematurely cancelled or detached from request metadata. This skill codifies patterns to avoid these pitfalls and to ensure cancellations, deadlines, and request-scoped values behave predictably across goroutines and network boundaries.

Core Features & Use Cases

  • Propagation rules: Always accept ctx as the first parameter and pass it through HTTP handlers, services, database calls, and client requests so cancellations and deadlines cascade correctly.
  • Cancellation and timeouts: Create cancellable children with WithCancel, WithTimeout, or WithDeadline and always defer cancel to free resources; design per-attempt timeouts and nested timeout hierarchies intentionally.
  • Context values & tracing: Store request-scoped metadata like trace IDs using unexported key types and provide typed getters; inject those values into outbound requests for observability.
  • Background work patterns: Use context.WithoutCancel (Go 1.21+) to preserve values while detaching cancellation for audit logs or async enqueues; use context.AfterFunc for non-blocking cleanup callbacks.
  • APIs and linters: Prefer *Context API variants (QueryContext, ExecContext, NewRequestWithContext) and enable staticcheck/govet rules to catch common mistakes.
  • Use Cases: building HTTP handlers that cancel downstream work on client disconnect, repository layers that respect request timeouts, retry logic that scopes timeouts per attempt, and launching audit or cleanup tasks that retain trace metadata.

Quick Start

Inspect a failing Go HTTP handler and update it to obtain the request context from the incoming request, pass that ctx to all downstream database and HTTP client calls, wrap operations with context.WithTimeout and defer cancel for per-attempt deadlines, and use context.TODO as a temporary placeholder when migrating functions.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I prevent context resource leaks in Go HTTP servers?

To prevent context resource leaks in Go HTTP servers, always defer cancel when using WithCancel, WithTimeout, or WithDeadline, and pass the request context as the first parameter to all downstream database and HTTP client calls.

Why does my Go background work get cancelled when the HTTP request ends?

Background work gets cancelled because child contexts inherit the parent request's cancellation signal. Use context.WithoutCancel (Go 1.21+) to detach cancellation while preserving request-scoped trace metadata for async audit logs or enqueues.

What is the best way to pass trace IDs in Go context values?

The best way to pass trace IDs in Go context values is using unexported key types and providing typed getters, which prevents key collisions and ensures request-scoped metadata propagates safely across network boundaries.

How do I scope per-attempt timeouts in Go retry logic?

To scope per-attempt timeouts in Go retry logic, wrap each individual operation with context.WithTimeout and defer cancel, creating nested timeout hierarchies that respect the overall request deadline.

Can I use context.TODO as a placeholder when migrating Go functions?

Yes, you can use context.TODO as a temporary placeholder when migrating Go functions to accept context parameters, allowing gradual adoption of context propagation without breaking existing call sites.

Does context.AfterFunc support non-blocking cleanup callbacks in Go?

Yes, context.AfterFunc supports non-blocking cleanup callbacks in Go 1.21+, registering functions that execute when a context is cancelled, which is useful for releasing resources without explicitly watching the Done channel.