golang-context

Fix context.Context propagation, cancellation, and request-scoped values in Go code.

2|Updated Mar 13, 2023
One-click install
npx skills add https://github.com/haipham22/golang-sample --skill golang-context-haipham22
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/haipham22/golang-sample/tree/main/.agents/skills/golang-context
Command: npx skills add https://github.com/haipham22/golang-sample --skill golang-context-haipham22

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you correctly design, audit, and fix context.Context usage across Go applications so cancellation, deadlines, and request-scoped data flow cleanly through every layer.

Core Features & Use Cases

  • Propagation Discipline: Keep the same context moving from HTTP handlers to services, databases, and external APIs.
  • Cancellation and Timeouts: Apply WithCancel, WithTimeout, WithDeadline, and proper cancel cleanup to avoid leaks and stuck work.
  • Request-Scoped Values: Use unexported keys for trace IDs, tenant IDs, and other metadata without polluting function parameters.
  • Background Work: Use WithoutCancel when asynchronous work must survive past the request lifecycle.
  • Practical Review Support: Catch common Go mistakes such as storing context in structs, using Background in the middle of a request, or forgetting context-aware database and HTTP calls.

Quick Start

Use the golang-context skill to review this Go code and fix its context propagation, cancellation, timeout, and request-value handling.

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 cancellation across HTTP handlers and databases?

Propagate Go context cancellation by passing the context as the first argument in function signatures and using r.Context in HTTP handlers to ensure deadlines and cancellation signals flow cleanly to databases and outbound API calls.

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

Your Go background work gets cancelled because it is tied to the request context. Use context.WithoutCancel to detach the asynchronous work so it survives past the request lifecycle without triggering premature cancellation.

What is the best way to pass request-scoped values in Golang without polluting function parameters?

The best way to pass request-scoped values in Golang is using context.WithValue with unexported key types for metadata like trace IDs, which keeps the data scoped to the request without polluting function parameters.

How do I fix context leaks caused by WithTimeout or WithCancel in Go?

Fix context leaks in Go by calling the generated cancel function immediately after the context.WithTimeout or WithCancel operation, ensuring proper cleanup to avoid stuck work and leaked goroutines during code review.

Can I store a Go context inside a struct for later use?

You should not store a Go context inside a struct because it obscures lifecycle management and breaks propagation discipline; contexts should be passed explicitly to functions to maintain cancellation and deadline integrity.

How do I migrate legacy Go APIs that do not accept a context parameter?

Migrate legacy Go APIs by introducing context.TODO for incomplete migrations and gradually updating function signatures to accept context.Context as the first parameter, ensuring the codebase compiles during the transition.