golang-context

Propagate cancellation and deadlines across Go code with context.Context.

1|Updated Apr 4, 2026
One-click install
npx skills add https://github.com/Jylhis/claude-marketplace --skill golang-context-jylhis
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/Jylhis/claude-marketplace/tree/main/plugins/golang-dev/skills/golang-context
Command: npx skills add https://github.com/Jylhis/claude-marketplace --skill golang-context-jylhis

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go developers often struggle to implement idiomatic context.Context usage across handlers, services, and persistence layers, leading to leaked goroutines and flaky cancellation behavior.

Core Features & Use Cases

  • Guidance on creating, passing, and canceling contexts using WithCancel, WithTimeout, WithDeadline, and WithoutCancel.
  • Practices for avoiding storing context in structs and for using unexported keys to carry request-scoped metadata.
  • Examples spanning HTTP handlers, service layers, and repository/database calls.

Quick Start

Create a small HTTP handler that propagates r.Context() through a service and repository using the *_Context variants to demonstrate cancellation on client disconnect.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I use Go context to stop goroutines when an HTTP client disconnects?

To stop goroutines on client disconnect, pass r.Context() from the HTTP handler through your service and repository layers. This propagates cancellation signals automatically, ensuring downstream database calls stop executing when the client connection drops.

When should I use context.WithTimeout versus context.WithDeadline in Go?

Use context.WithTimeout when you need to cancel operations after a specific duration, and context.WithDeadline when targeting an absolute time. Both ensure that resource-intensive Go routines terminate reliably, preventing leaks when calls exceed expected timeframes.

What is the best way to pass request-scoped metadata using Golang context values?

The best way to pass request-scoped metadata is by using unexported keys for context values. This prevents key collisions across packages and ensures that metadata is carried safely without violating the idiomatic rule of avoiding storing context in structs.

Why does my Go context cancellation leak goroutines across service layers?

Goroutine leaks during context cancellation usually occur when contexts are not propagated correctly or stored in structs. Apply context variants like WithCancel and WithoutCancel properly across HTTP handlers and services to ensure cancellation signals reach all routines.

Can I use context.WithoutCancel to detach a Go operation from a parent request?

Yes, you can use context.WithoutCancel in Go to detach an operation from its parent context. This allows background tasks to continue running independently even after the originating HTTP request or parent process has been canceled or timed out.

Does Golang context work with standard database calls to enforce timeouts?

Yes, Golang context works with database calls by enforcing timeouts and cancellation. Use the proper context variants like QueryContext and ExecContext instead of standard methods to ensure queries terminate immediately when a context deadline exceeds or cancels.