golang-context
Idiomatic context.Context usage in Golang — propagation through API boundaries, cancellation, timeouts and deadlines, request-scoped values…
Skill by samber
Install
Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.
npx agentmarket install skill/hellopoisonx-golang-context
Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.
Get the weekly diff
The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.
About
Use cases: backend, productivity
Reviews
Write a reviewNo reviews yet. Installed it? Reviews come from verified installers only. How reviews work
Permissions
What this skill can do and what it asks for, from its SKILL.md. Checked Oct 25, 2026.
- Tools
- No tools found
- Secrets
- None asked for
- Install scripts
- None found
Network hosts it talks to 1
codeload.github.com· found in install command
Files it touches
~/.claude/skills/golang-context
Tools it pre-approves 9
From allowed-tools in SKILL.md: the agent may use these without asking you each time.
ReadEditWriteGlobGrepBash(go:*)Bash(golangci-lint:*)Bash(git:*)Agent
What's inside
- Skill name
golang-context- Description
- Idiomatic context.Context usage in Golang — propagation through API boundaries, cancellation, timeouts and deadlines, request-scoped values, context.WithoutCancel for background work outliving requests. Apply when designing context propagation across layers, debugging leaked or unexpired contexts, choosing between context.Background/TODO/WithoutCancel, or storing values in context. Not for code that merely accepts ctx as first parameter.
- License
- MIT
- Compatibility
- Designed for Claude Code or similar AI coding agents, and for projects using Golang.
- Allowed tools
- Read, Edit, Write, Glob, Grep, Bash(go:*), Bash(golangci-lint:*), Bash(git:*), Agent
- Metadata
- author: samber · version: 1.2.1
- Instructions
- 66 lines in SKILL.md · view on GitHub
Before installing
- Pre-approves some shell commands
Files (5)
- SKILL.md
- evals.json · evals/
- cancellation.md · references/
- http-services.md · references/
- values-tracing.md · references/
SKILL.md (first lines)
> **Community default.** A company skill that explicitly supersedes `samber/cc-skills-golang@golang-context` skill takes precedence.
# Go context.Context Best Practices
`context.Context` is Go's mechanism for propagating cancellation signals, deadlines, and request-scoped values across API boundaries and between goroutines. Think of it as the "session" of a request — it ties together every operation that belongs to the same unit of work.
## Best Practices Summary
1. The same context MUST be propagated through the entire request lifecycle: HTTP handler → service → DB → external APIs
2. `ctx` MUST be the first parameter, named `ctx context.Context`
3. NEVER store context in a struct — pass explicitly through function parameters
4. NEVER pass `nil` context — use `context.TODO()` if unsure
5. `cancel()` MUST be called on all control-flow paths for `WithCancel`/`WithTimeout`/`WithDeadline`, unless ownership of the context and cancel function is explicitly returned or transferred
6. `context.Background()` MUST only be used at the top level (main, init, tests)
7. **Use `context.TODO()`** as a placeholder when you know a context is needed but don't have one yet
8. NEVER create a new `context.Background()` in the middle of a request path
9. Context value keys MUST be unexported types to prevent collisions
10. Context values MUST only carry request-scoped metadata — NEVER function parameters
11. **Use `context.WithoutCancel`** (Go 1.21+) when spawning background work that must outlive the parent request
## Creating Contexts
| Situation | Use |
| --- | --- |
| Entry point (main, init, test) | `context.Background()` |
| Function needs context but caller doesn't provide one yet | `context.TODO()` |
| Inside an HTTP handler | `r.Context()` |
| Need cancellation control | `context.WithCancel(parentCtx)` |
| Need a deadline/timeout | `context.WithTimeout(parentCtx, duration)` |
## Context Propagation: The Core Principle
The most important rule: **propagate the same context through the entire call chain**. When you propagate correctly, cancelling the parent context cancels all downstream work automatically.
```go
// ✗ Bad — creates a new context, breaking the chain
func (s *OrderService) Create(ctx context.Context, order Order) error {
return s.db.ExecContext(context.Background(), "INSERT INTO orders ...", order.ID)
}
// ✓ Good — propagates the caller's context
func (s *OrderService) Create(ctx context.Context, order Order) error {
return s.db.ExecContext(ctx, "INSERT INTO orders ...", order.ID)
}
```
## Deep Dives
- **[Cancellation, Timeouts & Deadlines](./references/cancellation.md)** — How cancellation propagates: `WithCancel` for manual cancellation, `WithTimeout` for automatic cancellation after a duration, `WithDeadline` for absolute time deadlines. Patterns for listening (`<-ctx.Done()`) in concurrent code, `AfterFunc` callbacks, and `WithoutCancel` for operations that must outlive their parent request (e.g., audit logs).
- **[Context Values & Cross-Service Tracing](./references/values-tracing.md)** — Safe context value patterns: unexported key types to prevent namespace collisions, when to use context values (request ID, user ID) vs function parameters. Trace context propagation: OpenTelemetry trace headers, correlation IDs for log aggregation, and marshaling/unmarshaling context across service boundaries.
- **[Context in HTTP Servers & Service Calls](./references/http-services.md)** — HTTP handler context: `r.Context()` for request-scoped cancellation, middleware integration, and propagating to services. HTTP client patterns: `NewRequestWithContext`, client timeouts, and retries with context awareness. Database operations: always use `*Context` variants (`QueryContext`, `ExecContext`) to respect deadlines.
## Cross-References
- → See the `samber/cc-skills-golang@golang-concurrency` skill for goroutine cancellation patterns using context
- → See the `samber/cc-skills-golang@golang-database` skill for context-aware database operations (QueryContext, ExecContext)
- → See the `samber/cc-skills-golang@golang-observability` skill for trace context propagation with OpenTelemetry
- → See the `samber/cc-skills-golang@golang-design-patterns` skill for timeout and resilience patterns
## Enforce with Linters
Many context pitfalls are caught automatically by linters: `govet`, `staticcheck`. → See the `samber/cc-skills-golang@golang-lint` skill for configuration and usage.From hellopoisonx/aim, MIT.
More from hellopoisonx/aim
- 0.534score
golang-benchmark
samber
Golang benchmarking, profiling, and performance measurement.
SkillCFreshness: C★ 5Claude Code - 0.534score
golang-code-style
samber
Golang code style conventions — line length and breaking, variable declarations, control flow clarity, when comments help vs hurt.
SkillCFreshness: C★ 5Claude Code +9 - 0.534score
golang-continuous-integration
samber
CI/CD pipeline configuration using GitHub Actions for Golang projects — testing, linting, SAST, security scanning, code coverage, Dependabot, Renovate…
SkillCFreshness: C★ 5Claude Code - 0.534score
golang-concurrency
samber
Golang concurrency patterns.
SkillCFreshness: C★ 5Claude Code +9 - 0.529score
golang-cli
samber
Golang CLI application development.
SkillCFreshness: C★ 5Claude Code +9 - 0.526score
aim-dev-tool
hellopoisonx
AIM 开发测试工具(dev-tool)。Python CLI,覆盖 gateway 全部 REST 端点与 WebSocket 帧协议,支持多 profile 多用户并行测试、并发压测。当需要调试 REST/WS 接口、运行集成测试、压力测试、或新增命令时使用。
SkillCFreshness: C★ 5Claude Code +9 - 0.524score
aim-docker-datastore
hellopoisonx
AIM 本地 Docker 数据存储运维 Skill。Use when the user asks to inspect, connect to, query, debug, or verify Docker Compose Kafka, Redis, or PostgreSQL in this project…
SkillCFreshness: C★ 5Claude Code +9 - 0.523score
aim-database-migration
hellopoisonx
AIM 数据库迁移规范与 Docker Compose 集成。当修改 migration SQL 文件、新增迁移、修改 docker-compose.yaml 的 migrate 服务、或排查迁移失败时使用。
SkillCFreshness: C★ 5Claude Code +9 - 0.523score
aim-ws-token-management
hellopoisonx
WebSocket 连接 Token 生命周期管理。覆盖 Token 过期导致 WS 断连的排查与修复、JWT TTL 配置链路、WSClient 自动刷新机制、压测长跑场景下的 Token 续期策略。
SkillCFreshness: C★ 5Claude Code +9 - 0.523score
aim-repo-mapping
hellopoisonx
aim 仓库导航 Skill。用于快速判断需求应该落到哪个业务域、哪个控制器、哪个工作流或哪个配置文件;当需求入口不清晰、需要先找模块边界、接口总览、运行入口或改动影响面时使用。
SkillCFreshness: C★ 5Claude Code +9 - 0.520score
aim-proto-domain
hellopoisonx
aim 的 Protobuf 协议域。定义跨端/跨服务线缆协议。对应 `shared/proto` 目录。
SkillCFreshness: C★ 5Claude Code +9 - 0.520score
aim-shared-domain
hellopoisonx
aim 的进程内共享包域。对应 `app/shared` 模块。
SkillCFreshness: C★ 5Claude Code +9
Signals
- Stars
- 5
- Forks
- 0
- Open issues
- 0
- Last commit (this folder)
- May 26, 2026 (5 months ago)
From GitHub (fetched Oct 24, 2026).
Details
- Version
- 1.2.1
- License
- MIT
- Repository
- github.com/hellopoisonx/aim
- First seen
- Oct 24, 2026
- Last checked
- Oct 24, 2026
- Source
- GitHubgithub-graphql
More from samber
- 0.563score
golang-design-patterns
samber
Idiomatic Golang design patterns — functional options, constructors, error flow and cascading, resource management and lifecycle, graceful shutdown…
SkillBFreshness: B★ 2Claude Code +9 - 0.563score
golang-performance
samber
Golang performance optimization patterns and methodology - if X bottleneck, then apply Y.
SkillBFreshness: B★ 2Claude Code - 0.563score
golang-security
samber
Security best practices and vulnerability prevention for Golang.
SkillBFreshness: B★ 2Claude Code - 0.563score
golang-testing
samber
Production-ready Golang tests — table-driven tests, testify suites and mocks, parallel tests, fuzzing, fixtures, goroutine leak detection with goleak…
SkillBFreshness: B★ 2Claude Code +9 - 0.563score
golang-documentation
samber
Comprehensive documentation guide for Golang projects, covering godoc comments, README, CONTRIBUTING, CHANGELOG, Go Playground, Example tests, API docs, and…
SkillBFreshness: B★ 2Claude Code - 0.534score
golang-benchmark
samber
Golang benchmarking, profiling, and performance measurement.
SkillCFreshness: C★ 5Claude Code
Cite this page
golang-context skill. Agent Mart. https://agentmart.dev/skills/hellopoisonx-golang-context (last updated 2026-10-24)