golang-context

Enforce correct Go context.Context propagation, cancellation, deadlines, and request-scoped values.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill prevents broken cancellation, leaked resources, and lost request metadata in Go applications by enforcing correct use of context.Context across handlers, service layers, databases, and outbound calls.

Core Features & Use Cases

  • Correct context propagation across API boundaries: ensures the same ctx flows from entry points through every downstream operation.
  • Cancellation, deadlines, and timeouts done safely: applies context.WithCancel/WithTimeout/WithDeadline patterns with proper cancel() handling to avoid leaks.
  • Safe use of request-scoped values: recommends unexported key types for context.WithValue and limits values to request metadata (e.g., trace IDs).
  • Background work that outlives requests: uses context.WithoutCancel for goroutines that must continue after the request completes, while preserving context values.
  • Go 1.21+ cleanup and lifecycle tooling: covers context.AfterFunc for non-blocking cleanup on cancellation.

Quick Start

Apply the golang-context Skill to review or refactor your Go call chain so every operation uses the incoming request context and respects cancellation and deadlines.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I fix broken Go context cancellation chains across HTTP handlers and service layers?

Fix broken Go context cancellation chains by enforcing ctx-first parameter conventions and ensuring the same context flows from entry points through every downstream operation. This prevents leaked resources and lost cancellation signals in request processing.

How do I safely pass request-scoped values with context in Go without key collisions?

Safely pass request-scoped values in Go by using unexported key types for context.WithValue and limiting values to request metadata like trace IDs. This approach prevents key collisions and maintains type safety across API boundaries.

How do I run background goroutines in Go that outlive the request context?

Run background goroutines that outlive requests by using context.WithoutCancel, which allows work to continue after the request completes while preserving context values. This prevents premature cancellation of necessary cleanup or async tasks.

How do I handle context timeout and cancel cleanup in Go 1.21+ without blocking?

Handle non-blocking cleanup on cancellation in Go 1.21+ by using context.AfterFunc. This registers a callback executed when the context is cancelled, allowing graceful resource release without blocking the main goroutine.

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

Prevent context leaks in Go by calling the cancel function returned by context.WithTimeout or WithCancel immediately when the operation completes. This ensures derived contexts are properly cleaned up and avoids lingering goroutines.

How do I use context correctly with database calls in Go?

Use context correctly with database calls in Go by passing the request context to QueryContext or ExecContext methods. This ensures database operations respect cancellation and deadlines, properly terminating long-running queries when the request is cancelled.