go-context

Review Go code for correct context.Context usage and cancellation handling.

64|9|Updated Mar 27, 2026
One-click install
npx skills add https://github.com/eduardo-sl/go-agent-skills --skill go-context-eduardo-sl
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-context
Source: https://github.com/eduardo-sl/go-agent-skills/tree/main/skills/%28code-quality%29/go-context
Command: npx skills add https://github.com/eduardo-sl/go-agent-skills --skill go-context-eduardo-sl

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers correctly use context.Context across Go codebases to avoid goroutine leaks, orphaned work, incorrect cancellations, and deadline mismanagement that cause subtle production failures.

Core Features & Use Cases

  • Parameter positioning and signatures: Ensure context is always the first parameter in functions and methods that accept it.
  • Cancellation and timeouts: Guidance on deferring cancels, using WithTimeout/WithDeadline appropriately, and allocating timeout budgets for child operations.
  • Context values and HTTP handling: Best practices for request-scoped values using unexported key types with accessors, and how to use r.Context() in HTTP handlers and middleware.
  • Use Case: Audit an HTTP service to add context timeouts for database queries, ensure middleware attaches request IDs safely, and verify workers respect cancellation to prevent leaks.

Quick Start

Review the following Go code and correct context usage by ensuring context is the first parameter, adding deferred cancels where needed, applying appropriate timeouts and deadlines, and replacing any stored context fields.

Frequently Asked Questions about go-context

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

FAQPage Schema
How do I prevent goroutine leaks in Go services?

Prevent goroutine leaks in Go services by correctly using context.Context to propagate cancellation signals to child operations. Ensure functions accept context as the first parameter and always defer cancel functions to release resources when operations complete or timeout.

How do I use context timeouts and deadlines in Go HTTP handlers?

Use context timeouts and deadlines in Go HTTP handlers by calling r.Context() and applying WithTimeout or WithDeadline. Allocate timeout budgets for child operations like database queries to ensure they cancel properly and prevent orphaned work.

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

Pass request-scoped values in Go context by using unexported key types paired with accessor functions. This prevents key collisions and ensures safe retrieval of values like request IDs when propagating context through middleware and handlers.

Why should context.Context be the first parameter in Go functions?

context.Context should be the first parameter in Go functions to standardize context propagation and ensure cancellation signals reach all operations. Storing context in structs is discouraged because it leads to improper cancellation and unclear lifetimes.

Does this Go context guidance apply to background workers and tests?

Yes, this Go context guidance applies to background workers and tests. It ensures background workers respect cancellation to prevent leaks, while tests can verify proper context propagation, timeout enforcement, and cancellation behavior across services and libraries.

What are common causes of improper cancellation in Go programs?

Improper cancellation in Go programs commonly stems from not deferring cancel functions, storing context fields in structs, or ignoring ctx.Err(). Checking ctx.Err() and correctly applying WithTimeout prevents subtle production failures from deadline mismanagement.