golang-context

Repair Go context.Context cancellation, deadline propagation, and value practices across call chains.

2|Updated Feb 12, 2024
One-click install
npx skills add https://github.com/adibfirman/dotfiles --skill golang-context-adibfirman
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/adibfirman/dotfiles/tree/main/claude/.claude/skills/technical/golang/golang-context
Command: npx skills add https://github.com/adibfirman/dotfiles --skill golang-context-adibfirman

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Prevents broken cancellation, leaking timers, and incorrect request-scoped value handling by enforcing idiomatic context.Context propagation across Go API boundaries.

Core Features & Use Cases

  • Correct context propagation: Keep the same ctx through the full call chain (HTTP handler → services → DB → external calls) to ensure cancellations and deadlines work end-to-end.
  • Resource-safe derived contexts: Use context.WithCancel, context.WithTimeout, and context.WithDeadline with guaranteed cancel() calls to avoid leaked timers and lingering goroutines.
  • Safe request-scoped values: Store only request-scoped metadata in context values using unexported key types to avoid collisions, and use context.WithoutCancel for background work that must outlive the request.
  • Practical guidance for Go codebases: Includes actionable rules for ctx placement (first parameter), placeholder choice (context.TODO vs context.Background), HTTP/client/db context-aware APIs, and common failure modes.

Quick Start

Ask your AI coding agent to refactor your Go code to propagate request context correctly from handlers through database and HTTP client calls, including proper WithTimeout cancel() usage and safe context values.

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 safely across HTTP handlers and service layers?

To propagate Go context safely, pass context.Context as the first parameter through the entire call chain from HTTP handlers to services and database queries. This ensures cancellations and deadlines work end-to-end without breaking API boundaries.

Why do my Go context timeouts cause leaked timers and lingering goroutines?

Go context timeouts cause leaked timers when derived contexts from WithTimeout or WithCancel do not call the returned cancel function. You must use a deferred cancel() call immediately after creating the derived context to release resources promptly.

What is the best way to store request-scoped values in Go context?

The best way to store request-scoped values in Go context is by using unexported key types. This prevents key collisions with other packages and ensures that only request metadata is stored, keeping the context chain safe and idiomatic.

When should I use context.Background vs context.TODO in Go?

Use context.Background when you intentionally want a non-cancelable root context, such as for background tasks. Use context.TODO when you are unsure which context to use or when a function lacks a context parameter during refactoring.

Can I run background tasks that outlive a Go HTTP request context?

Yes, you can use context.WithoutCancel to create a new context that outlives the HTTP request. This allows background tasks and goroutines to continue executing independently of the parent request's cancellation or deadline.

What are the common failure modes when using Go context in database and outbound HTTP calls?

Common Go context failure modes include passing nil contexts, not using context-aware API variants for database and outbound HTTP calls, and breaking the propagation chain. These prevent timeouts and cancellations from reaching downstream operations effectively.