golang-context

Review Go code for correct context.Context propagation and cancellation.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill prevents common Go errors with context.Context, like losing cancellation, leaking derived contexts, misusing context values, or breaking tracing across API boundaries.

Core Features & Use Cases

  • Correct context propagation across HTTP handlers, services, databases, and outbound HTTP calls.
  • Cancellation, deadlines, and timeouts that respect parent-child behavior and always clean up via cancel().
  • Request-scoped values using unexported key types for trace/correlation data, while keeping infrastructure and business inputs as explicit parameters.
  • Background work patterns using context.WithoutCancel for tasks that must outlive the request, plus context.AfterFunc for non-blocking cleanup.

Quick Start

Ask an AI coding agent to review your Go code for context misuse and refactor it to consistently pass ctx, use context-aware APIs, and correctly handle cancellation, deadlines, and 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 correctly across HTTP handlers and database calls?

Correct Go context propagation requires passing the same ctx through the call chain and using context-aware APIs like *Context SQL methods to maintain cancellation and tracing across application layers.

Why does my Go context cancellation leak or not trigger cleanup?

Context cancellation leaks occur when derived contexts are not cleaned up; always defer the cancel function for derived contexts to ensure resources are released when parent deadlines or timeouts expire.

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

Use context.WithoutCancel for tasks that must outlive the request, and apply context.AfterFunc for non-blocking cleanup to ensure background work continues safely after the parent context is cancelled.

What is the best way to pass request-scoped values in Go without misusing context?

Pass request-scoped values using unexported key types with WithValue for trace data, while keeping infrastructure and business inputs as explicit parameters to avoid context misuse.

Can I use context.Background in the middle of a Go HTTP request?

Avoid using context.Background mid-request as it breaks cancellation propagation and tracing across API boundaries; instead, propagate the existing request context through the call chain.

How do I add timeouts and deadlines to outbound HTTP requests in Go?

Add timeouts and deadlines to outbound HTTP requests by deriving a context with timeout and using http.NewRequestWithContext, ensuring the request respects parent-child cancellation behavior.