golang-context

Explain context.Context usage for cancellation, propagation, and request-scoped values in Golang.

Updated May 30, 2026
One-click install
npx skills add https://github.com/ozan-fn/mqtt-capture --skill golang-context-ozan-fn
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/ozan-fn/mqtt-capture/tree/main/.agents/skills/golang-context
Command: npx skills add https://github.com/ozan-fn/mqtt-capture --skill golang-context-ozan-fn

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill provides guidance on idiomatic usage of context.Context in Golang, ensuring efficient cancellation, propagation, and management of request-scoped values across API boundaries and goroutines.

Core Features & Use Cases

  • Context Propagation: Ensures proper context propagation across the entire request lifecycle.
  • Cancellation and Deadlines: Best practices for managing context cancellation and timeouts.
  • Use Cases: Ideal for developers designing systems with context propagation, debugging context leaks, choosing the right context variants, or managing request-scoped values.

Quick Start

Run the golang-context skill to learn about the proper usage of context.Context in Golang.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I properly propagate context.Context across API boundaries and goroutines in Golang?

Proper context.Context propagation in Golang requires passing the context as the first argument to functions and ensuring it flows through the entire request lifecycle across API boundaries and goroutines. This Skill provides best practices for maintaining unbroken context chains.

What are the best practices for managing context cancellation and timeouts in Go?

Best practices for context cancellation and timeouts in Go involve using context.WithCancel and context.WithTimeout to manage deadlines, ensuring goroutines listen for the Done channel to prevent leaks. This Skill covers idiomatic cancellation management across services.

When should I use request-scoped values with context.Context in Golang?

Request-scoped values in Golang context.Context should be used for cross-service tracing data and request metadata, not for optional function parameters. This Skill explains when and how to manage these values across API boundaries effectively.

Why does my Golang context cancellation not work across nested goroutines?

Golang context cancellation fails across nested goroutines when the parent context is not correctly propagated or when derived contexts are not properly linked. This Skill provides debugging guidelines for context leaks and ensures correct cancellation propagation.

How do I choose the right context.Context variant for my Golang application?

Choosing the right context.Context variant in Golang depends on whether you need background tasks, request-scoped data, cancellation signals, or timeouts. This Skill provides guidelines for selecting the appropriate context variant for your system design.