golang-context

Implement Go context management for cancellation, timeouts, and trace propagation.

Updated May 4, 2026
One-click install
npx skills add https://github.com/VIethoangnguyenle/nexus-enterprise --skill golang-context-viethoangnguyenle
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/VIethoangnguyenle/nexus-enterprise/tree/main/.agent/skills/golang-context
Command: npx skills add https://github.com/VIethoangnguyenle/nexus-enterprise --skill golang-context-viethoangnguyenle

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) and scripts (resource) components.

What problem does it solve?

Managing request lifecycles and cross-service communication in Go applications using idiomatic context handling.

Core Features & Use Cases

  • Context propagation best practices: Ensuring proper use of context to manage cancellations, timeouts, and request-scoped data.
  • Resource control: Demonstrating how to correctly create, derive, and cancel contexts; avoiding leaks and misuses.
  • Cross-service tracing and values: Facilitating trace ID propagation and safe request metadata management in HTTP servers and clients.
  • Use Case: Developing a scalable web service that gracefully handles client disconnects, timeouts, and distributed tracing with minimal resource leaks.

Quick Start

Use this skill to implement context-aware HTTP handlers that propagate request contexts to database and external API calls following Go best practices.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I use Golang context to handle client disconnects and timeouts in HTTP servers?

Golang context manages request lifecycles in HTTP servers by propagating cancellation signals and timeouts downstream. You can implement context-aware handlers that automatically terminate database or external API calls when a client disconnects, preventing resource leaks.

How does context propagation work for distributed tracing in Go microservices?

Context propagation facilitates distributed tracing in Go microservices by carrying trace IDs and request-scoped metadata across service boundaries. Proper context handling ensures trace IDs propagate seamlessly through HTTP clients and servers, maintaining end-to-end request visibility.

What is the best way to avoid context leaks when managing concurrency in Go?

Avoiding context leaks in Go concurrency requires correctly deriving and canceling contexts. By using idiomatic patterns to create child contexts and ensuring cancellation functions are called, you prevent goroutines from blocking indefinitely and consuming unnecessary resources.

Can I use AfterFunc and WithoutCancel for advanced context management in Go?

Yes, AfterFunc and WithoutCancel are advanced context features in Go. AfterFunc registers a callback to execute when a context is canceled, while WithoutCancel creates a new context that ignores the parent's cancellation, useful for background cleanup tasks.

When do I need to implement context management for cross-service communication in Go?

You need context management for cross-service communication when building scalable Go web services. It ensures safe request metadata handling, enforces timeouts across external system interactions, and gracefully manages concurrent routines during distributed operations.