asarchami/dotfiles/go-context

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Go context.Context — propagate the request's session: creation, propagation, cancellation, timeouts, deadlines, request-scoped values, trace context. Use when writing or reviewing any Go code that takes or passes context, touches a database, or spawns goroutines.

Install

npx vskill@latest install asarchami/dotfiles/go-context

SKILL.md

Go Context

Leading word: propagate. context.Context is the request's session — the cancellation signal, deadline, and request-scoped data that every operation in the same unit of work shares. The discipline is one verb: propagate the same context through the whole chain, and derive from it only with a deferred cancel.

Steps

  1. Take ctx first. Every function that reaches a database, an external API, or a goroutine takes ctx context.Context as its first parameter.

Done when: no function on the call path reaches an external side effect without receiving ctx.

  1. Propagate, don't recreate. Pass the caller's context down the chain. context.Background() belongs only at entry points (main, init, tests); context.TODO() is a placeholder while wiring.

Done when: no `context.Background()` or `context.TODO()` appears in the middle of a request path.

  1. Derive with a deferred cancel. WithCancel for manual control, WithTimeout for a duration, WithDeadline for an absolute time — and defer cancel() on the same line you derive.

Done when: every derived context has its cancel deferred immediately; `go vet` reports nothing.

  1. Bound the values. Carry only request-scoped metadata (request ID, user ID) as context values under an unexported key type. Function parameters and struct fields are the wrong home for them.

Done when: every value key is an unexported type and the value is request metadata, not a parameter.

  1. Start from the source. Handlers use r.Context(); database calls use the *Context variants (QueryContext, ExecContext); client calls use NewRequestWithContext.

Done when: the request's context reaches every downstream call without a fresh Background or TODO.

  1. Detach with intent. Work that must outlive the request roots itself in the parent context with context.WithoutCancel (Go 1.21+) — never a newly created Background.

Done when: background work is detached via WithoutCancel, not disconnected.

Reference

SituationCreate
Entry point (main, init, test)context.Background()
Caller doesn't provide one yetcontext.TODO()
Inside an HTTP handlerr.Context()
Manual cancellationcontext.WithCancel(parent)
Deadline or timeoutcontext.WithTimeout(parent, d) / context.WithDeadline(parent, t)
  • [Cancellation, Timeouts & Deadlines](./references/cancellation.md) — propagation mechanics, <-ctx.Done(), AfterFunc, WithoutCancel
  • [Values & Cross-Service Tracing](./references/values-tracing.md) — unexported keys, OpenTelemetry trace headers, correlation IDs
  • [HTTP Servers & Service Calls](./references/http-services.md) — handler context, client timeouts, *Context DB variants

Watch for

FailurePositive
context.Background() mid-requestPropagate the caller's context
nil context passedcontext.TODO() when uncertain
Context stored in a structPass explicitly through parameters
cancel() not deferreddefer cancel() on the derivation line
Exported value key (collision-prone)Unexported key type

Cross-references

  • → See go-concurrency for goroutine cancellation with context
  • → See go-database for QueryContext / ExecContext
  • → See go-observability for OpenTelemetry trace propagation
  • → See go-patterns for timeout and resilience patterns
  • → See go-lint for govet / staticcheck enforcement

Security

  • Security scanPassed, 90 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

7-day trend score
0

Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/asarchami/dotfiles/go-context/badge)](https://verified-skill.com/skills/asarchami/dotfiles/go-context)

Something wrong with this skill or its page? Tell us.