golang-context

Enforces GoRsquops; please check your input.

Updated Oct 1, 2024
One-click install
npx skills add https://github.com/mrlorentx/.files --skill golang-context-mrlorentx
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/mrlorentx/.files/tree/main/ai/claude/skills/golang-context
Command: npx skills add https://github.com/mrlorentx/.files --skill golang-context-mrlorentx

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Idiomatic context.Context usage across Go codebases—propagating cancellation, deadlines, and request-scoped values across components, services, and goroutines.

Core Features & Use Cases

  • Creation and propagation patterns: context.WithCancel, context.WithTimeout, context.WithDeadline, and passing ctx through function calls.
  • Safe value propagation: using unexported keys and avoiding storing context in structs; using r.Context() in HTTP handlers.
  • Cross-service tracing and observability: propagating trace/identity data through service boundaries.

Quick Start

Implement a minimal Go HTTP handler that creates a context with timeout and passes it to a downstream function.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I propagate cancellation signals across Go services using context?

Use context.WithCancel or context.WithTimeout to create a cancellable context, then pass it to goroutines. When the parent context is canceled, all derived contexts automatically propagate the cancellation signal to running goroutines.

How do I set a timeout for an HTTP handler in Go using context?

Use context.WithTimeout or context.WithDeadline to set expiration times on the context.Context, then pass it to all downstream database and HTTP calls. The context automatically cancels pending operations when the timeout or deadline expires.

Why should I avoid storing context in a struct in Go?

Avoid storing context.Context inside Go structs because contexts are designed to flow through function calls, not persist as struct state. Always pass context as the first parameter to maintain safe cancellation and value propagation.

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

Use unexported key types for context.WithValue to prevent key collisions and avoid storing context in structs. This approach keeps request-scoped values safe and idiomatic across service boundaries.

How do I create a Go HTTP handler with context timeout and pass it downstream?

Call r.Context() to get the request-scoped context, then use context.WithTimeout to create a derived context with a deadline. Pass this derived context to all downstream service and database calls to enforce timeouts.

How do I cancel goroutines with context in Go?

Use context.WithCancel or context.WithTimeout to create a cancellable context, then pass it to goroutines. When the parent context is canceled, all derived contexts automatically propagate the cancellation signal to running goroutines.