golang-context

Review Go code for correct context.Context propagation across API boundaries.

1|Updated May 26, 2026
One-click install
npx skills add https://github.com/tokiou/caba-inseguridad-v2-be --skill golang-context-tokiou
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/tokiou/caba-inseguridad-v2-be/tree/main/.claude/skills/golang-context
Command: npx skills add https://github.com/tokiou/caba-inseguridad-v2-be --skill golang-context-tokiou

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Prevents broken cancellation, missing deadlines, and incorrect context value handling by teaching idiomatic propagation of Go context.Context across API boundaries.

Core Features & Use Cases

  • Request-lifecycle propagation: Ensures the same ctx flows from HTTP handlers through services and into databases and outbound HTTP calls.
  • Cancellation, deadlines, and timeouts: Covers WithCancel, WithTimeout, WithDeadline, and correct cancel() handling to avoid leaks.
  • Request-scoped values & tracing: Shows safe context value patterns using unexported key types and how to carry trace/correlation IDs across services.
  • Background work correctness (Go 1.21+): Uses context.WithoutCancel for goroutines that must outlive the request while keeping context values.

Quick Start

Ask Claude to review a Go handler and service method for context propagation bugs (e.g., context.Background in handlers, missing defer cancel, improper context-aware DB/HTTP calls) and provide a corrected version with an explanation of each change.

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

Go context propagation prevents broken cancellation and missing deadlines by flowing the same ctx from HTTP handlers through services into databases, avoiding context.Background mid-request and ensuring derived contexts call cancel to prevent leaks.

How do I avoid context leaks when using WithTimeout or WithCancel in Go?

Avoid Go context leaks by calling the cancel function immediately via defer cancel() when using WithTimeout, WithCancel, or WithDeadline, ensuring resources are released when operations complete or parent contexts are abandoned.

When should I use context.WithoutCancel for background work in Go?

Use context.WithoutCancel in Go 1.21+ when you need background work to outlive an HTTP request while retaining context values, detaching the new context from the parent's cancellation and deadline signals.

What is the correct way to pass request-scoped values in Go context?

Pass request-scoped values in Go context safely by using unexported key types to store trace and correlation IDs, preventing key collisions and ensuring values propagate cleanly across API boundaries.

Why does my Go HTTP request hang when the client disconnects?

A Go HTTP request hangs on client disconnect if context propagation is broken, often caused by using context.Background mid-request instead of passing the handler's ctx to downstream services, database queries, and outbound HTTP calls.