go-context

Enforce context.Context as the first parameter across Go call chains.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go projects often struggle with structuring and propagating cancellation, deadlines, and values across API boundaries. This Skill provides patterns for using context.Context effectively, ensuring first-parameter context, avoiding embedding contexts in structs, and avoiding custom Context types, with guidance on where to attach data and how to derive and propagate context safely.

Core Features & Use Cases

  • Clear guidelines on making context the first parameter in functions and methods.
  • Rules against storing context in structs and against custom Context types.
  • Practical advice for deriving timeouts, deadlines, cancellation, and values, plus real-world examples.

Quick Start

Refactor code to pass context.Context as the first parameter across the call chain and remove embedded or custom contexts.

Frequently Asked Questions about go-context

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

FAQPage Schema
How do I pass context.Context through function calls in Go?

Pass context.Context as the first parameter in functions and methods to propagate it through the call chain. This convention ensures deadlines, cancellations, and values flow correctly across API boundaries without storing context in structs.

Why should I avoid storing context in a Go struct?

Storing context in structs breaks cancellation propagation and makes lifecycle management unpredictable. Context should flow explicitly through function parameters, ensuring derived timeouts and cancellation signals reach background workers and API handlers safely.

What is the best way to derive deadlines and cancellations in Golang?

Derive deadlines and cancellations by creating child contexts from parent contexts using built-in functions. This ensures that when a parent context is canceled, all derived child contexts are automatically canceled, propagating termination signals across modular packages.

Can I create a custom context type in Go for my API handlers?

No, avoid creating custom context types. Use the standard context.Context interface to ensure compatibility and safe value extraction. Custom implementations risk breaking cancellation propagation and traceability across background workers and API boundaries.

How do I safely attach and extract values in a Go context?

Attach values using context.WithValue and extract them safely using type-safe key extraction. This ensures traceability data flows across function calls without violating the convention of keeping context as the first parameter and avoiding struct storage.