golang-context

Propagate context.Context cancellation and deadlines across Go HTTP handlers and database calls.

7|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/Harmeet10000/skills --skill golang-context-harmeet10000
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/Harmeet10000/skills/tree/main/skills/backend/Golang/golang-context
Command: npx skills add https://github.com/Harmeet10000/skills --skill golang-context-harmeet10000

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Propagates and manages cancellation, deadlines, and request-scoped data in Go applications using idiomatic context.Context patterns.

Core Features & Use Cases

  • Propagation of context through HTTP handlers, services, and database calls to enable clean cancellation and timeouts.
  • Safe context value usage with unexported keys for tracing and request-scoped metadata.
  • Support for advanced patterns like WithoutCancel and context.AfterFunc for background work.

Quick Start

Refactor a sample HTTP handler to pass r.Context() through a DB call and an outbound HTTP request using context-aware APIs.

Frequently Asked Questions about golang-context

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

FAQPage Schema
How do I propagate context cancellation and deadlines across Go HTTP handlers?

Propagate context cancellation and deadlines across Go HTTP handlers by passing r.Context() through service calls and database queries. Use context-aware APIs like QueryContext to ensure work stops cleanly when the request is aborted or times out.

What is the best way to store request-scoped data in Golang context?

Store request-scoped data in Golang context using unexported keys. Avoid storing the context itself inside structs; instead, pass it explicitly as the first parameter to functions to maintain clean cancellation chains and traceability.

How do I use context to cancel database queries in Go?

Cancel database queries in Go by passing the request context to context-aware variants like QueryContext or ExecContext. This ensures the underlying database driver aborts the query automatically when the parent context deadline is exceeded or canceled.

Can I run background work after a Go context is canceled?

Run background work after a Go context is canceled using advanced patterns like context.WithoutCancel and context.AfterFunc. These allow you to detach new background operations from the parent request's cancellation signal while preserving request-scoped tracing data.

Why should I avoid storing Go context in a struct?

Avoid storing Go context in a struct because it breaks the explicit propagation chain required for reliable cancellation and deadlines. Passing context as the first function argument ensures all downstream service calls and HTTP requests respect timeout boundaries.

Does this Skill cover idiomatic context patterns for microservices?

This Skill covers idiomatic context patterns for microservices by applying context.Context across HTTP handlers, outbound service calls, and database queries. It enforces best practices for unexported keys and proper cleanup to ensure traceable and cancellable work.