[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7DNCM4ZC15SSDA5FAAHN5S0":3,"skill-same:01M7DNCM4ZC15SSDA5FAAHN5S0":69},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":15,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":24,"commitSha":25,"skillDirectory":26,"forksCount":13,"repositoryHtmlUrl":27,"repositoryHomepageUrl":28,"ownerHtmlUrl":29,"ownerBlogUrl":30,"ownerDescription":31,"updatedAt":32,"install":33,"resources":46},"01M7DNCM4ZC15SSDA5FAAHN5S0","samber-golang-context",null,"Design Go context propagation, cancellation, deadlines and request metadata. Load for operation-lifetime changes or context-related leaks after go-errors-and-context.","Cratis\u002FFundamentals.Go","Cratis","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F16456213?v=4","01M6BXRH290K4C8RS4PTKK3HXQ","01M7DN6YCMASAFGKYBBH3C1Z6E",0,"MIT","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-context\u002FLICENSE","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-context\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: samber-golang-context\ndescription: Design Go context propagation, cancellation, deadlines and request metadata. Load for operation-lifetime changes or context-related leaks after go-errors-and-context.\n---\n\n> Third-party guidance. Repository rules `.cratis\u002Fai\u002Frules\u002Fgo.md` and\n> `go-cratis-parity.md` take precedence where they conflict (e.g. Go minimum\n> version from go.mod, no `pkg\u002F`, functional options only when warranted).\n\n# Local review qualifications\n\n- Cancellation is a signal, not a join. Bound fan-out and wait for all owned work.\n- `WithoutCancel` also removes deadlines. The upstream detached audit example is\n  not suitable for an SDK operation: independent work needs an explicit owner,\n  budget, admission limit, stop and join path. Never detach to escape a caller.\n- `AfterFunc` stop does not wait for a running callback; resource release requires\n  synchronization with users and a documented ownership transfer.\n- HTTP JSON\u002FDB examples are sketches, not Cratis contracts. Check encode\u002Fclose\n  errors, preserve the response envelope, and close response bodies as the owner.\n- Trace headers are untrusted metadata, not authenticated identity. Validate and\n  bound them; use the agreed propagation protocol, not invented X-Trace headers.\n- Upstream cross-references name optional skills not all vendored here. Use the\n  repository-owned `go-concurrency` and transport skill instead; do not auto-install.\n\n# Go context.Context Best Practices\n\n`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.\n\n## Best Practices Summary\n\n1. Propagate the same context through the entire request lifecycle: HTTP handler → service → DB → external APIs — any link that starts a fresh context keeps working after the client is gone.\n2. Take `ctx` as the first parameter, named `ctx context.Context` — the fixed position is what makes context-aware APIs recognizable at a glance and what linters check.\n3. Pass context through function parameters instead of storing it in a struct — the struct outlives the request that filled it, so later calls reuse a context that is already cancelled or belongs to someone else.\n4. Pass `context.TODO()` rather than a `nil` context — `nil` panics on the first `Done()` or `Value()` call, far from the caller that passed it.\n5. Call `cancel()` on all control-flow paths for `WithCancel`\u002F`WithTimeout`\u002F`WithDeadline`, unless ownership of the context and cancel function is explicitly returned or transferred — an uncalled `cancel()` keeps the child attached to its parent and leaks its timer until the parent finishes.\n6. Create `context.Background()` only at top-level entry points (main, init, tests). Deeper in the call chain — especially mid-request — it detaches the work from the caller's deadline and cancellation, the propagation break shown below.\n7. Use `context.TODO()` as a placeholder when a context is needed but none exists yet — it marks the gap for a later fix instead of hiding it behind a `Background()` that looks deliberate.\n8. Declare context value keys as unexported types — with a plain `string` key, two packages using `\"user\"` silently overwrite each other.\n9. Carry only request-scoped metadata in context values, never function parameters — values retrieved through `Value()` lose compile-time typing and disappear from the function signature.\n10. Use `context.WithoutCancel` (Go 1.21+) when spawning background work that must outlive the parent request — otherwise the handler returning cancels the audit log or cleanup just started.\n\n## Creating Contexts\n\n| Situation | Use |\n| --- | --- |\n| Entry point (main, init, test) | `context.Background()` |\n| Function needs context but caller doesn't provide one yet | `context.TODO()` |\n| Inside an HTTP handler | `r.Context()` |\n| Need cancellation control | `context.WithCancel(parentCtx)` |\n| Need a deadline\u002Ftimeout | `context.WithTimeout(parentCtx, duration)` |\n\n## Context Propagation: The Core Principle\n\nThe 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.\n\n```go\n\u002F\u002F ✗ Bad — creates a new context, breaking the chain\nfunc (s *OrderService) Create(ctx context.Context, order Order) error {\n    _, err := s.db.ExecContext(context.Background(), \"INSERT INTO orders ...\", order.ID)\n    return err\n}\n\n\u002F\u002F ✓ Good — propagates the caller's context\nfunc (s *OrderService) Create(ctx context.Context, order Order) error {\n    _, err := s.db.ExecContext(ctx, \"INSERT INTO orders ...\", order.ID)\n    return err\n}\n```\n\n## Deep Dives\n\n- **[Cancellation, Timeouts & Deadlines](.\u002Freferences\u002Fcancellation.md)** — How cancellation propagates: `WithCancel` for manual cancellation, `WithTimeout` for automatic cancellation after a duration, `WithDeadline` for absolute time deadlines. Patterns for listening (`\u003C-ctx.Done()`) in concurrent code, `AfterFunc` callbacks, and `WithoutCancel` for operations that must outlive their parent request (e.g., audit logs).\n\n- **[Context Values & Cross-Service Tracing](.\u002Freferences\u002Fvalues-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\u002Funmarshaling context across service boundaries.\n\n- **[Context in HTTP Servers & Service Calls](.\u002Freferences\u002Fhttp-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.\n\n## Cross-References\n\n- → See the `samber\u002Fcc-skills-golang@golang-concurrency` skill for goroutine cancellation patterns using context\n- → See the `samber\u002Fcc-skills-golang@golang-database` skill for context-aware database operations (QueryContext, ExecContext)\n- → See the `samber\u002Fcc-skills-golang@golang-observability` skill for trace context propagation with OpenTelemetry\n- → See the `samber\u002Fcc-skills-golang@golang-design-patterns` skill for timeout and resilience patterns\n\n## Enforce with Linters\n\nMany context pitfalls are caught automatically by linters: `govet`, `staticcheck`. → See the `samber\u002Fcc-skills-golang@golang-lint` skill for configuration and usage.\n","> Third-party guidance. Repository rules `.cratis\u002Fai\u002Frules\u002Fgo.md` and\n> `go-cratis-parity.md` take precedence where they conflict (e.g. Go minimum\n> version from go.mod, no `pkg\u002F`, functional options only when warranted).\n\n# Local review qualifications\n\n- Cancellation is a signal, not a join. Bound fan-out and wait for all owned work.\n- `WithoutCancel` also removes deadlines. The upstream detached audit example is\n  not suitable for an SDK operation: independent work needs an explicit owner,\n  budget, admission limit, stop and join path. Never detach to escape a caller.\n- `AfterFunc` stop does not wait for a running callback; resource release requires\n  synchronization with users and a documented ownership transfer.\n- HTTP JSON\u002FDB examples are sketches, not Cratis contracts. Check encode\u002Fclose\n  errors, preserve the response envelope, and close response bodies as the owner.\n- Trace headers are untrusted metadata, not authenticated identity. Validate and\n  bound them; use the agreed propagation protocol, not invented X-Trace headers.\n- Upstream cross-references name optional skills not all vendored here. Use the\n  repository-owned `go-concurrency` and transport skill instead; do not auto-install.\n\n# Go context.Context Best Practices\n\n`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.\n\n## Best Practices Summary\n\n1. Propagate the same context through the entire request lifecycle: HTTP handler → service → DB → external APIs — any link that starts a fresh context keeps working after the client is gone.\n2. Take `ctx` as the first parameter, named `ctx context.Context` — the fixed position is what makes context-aware APIs recognizable at a glance and what linters check.\n3. Pass context through function parameters instead of storing it in a struct — the struct outlives the request that filled it, so later calls reuse a context that is already cancelled or belongs to someone else.\n4. Pass `context.TODO()` rather than a `nil` context — `nil` panics on the first `Done()` or `Value()` call, far from the caller that passed it.\n5. Call `cancel()` on all control-flow paths for `WithCancel`\u002F`WithTimeout`\u002F`WithDeadline`, unless ownership of the context and cancel function is explicitly returned or transferred — an uncalled `cancel()` keeps the child attached to its parent and leaks its timer until the parent finishes.\n6. Create `context.Background()` only at top-level entry points (main, init, tests). Deeper in the call chain — especially mid-request — it detaches the work from the caller's deadline and cancellation, the propagation break shown below.\n7. Use `context.TODO()` as a placeholder when a context is needed but none exists yet — it marks the gap for a later fix instead of hiding it behind a `Background()` that looks deliberate.\n8. Declare context value keys as unexported types — with a plain `string` key, two packages using `\"user\"` silently overwrite each other.\n9. Carry only request-scoped metadata in context values, never function parameters — values retrieved through `Value()` lose compile-time typing and disappear from the function signature.\n10. Use `context.WithoutCancel` (Go 1.21+) when spawning background work that must outlive the parent request — otherwise the handler returning cancels the audit log or cleanup just started.\n\n## Creating Contexts\n\n| Situation | Use |\n| --- | --- |\n| Entry point (main, init, test) | `context.Background()` |\n| Function needs context but caller doesn't provide one yet | `context.TODO()` |\n| Inside an HTTP handler | `r.Context()` |\n| Need cancellation control | `context.WithCancel(parentCtx)` |\n| Need a deadline\u002Ftimeout | `context.WithTimeout(parentCtx, duration)` |\n\n## Context Propagation: The Core Principle\n\nThe 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.\n\n```go\n\u002F\u002F ✗ Bad — creates a new context, breaking the chain\nfunc (s *OrderService) Create(ctx context.Context, order Order) error {\n    _, err := s.db.ExecContext(context.Background(), \"INSERT INTO orders ...\", order.ID)\n    return err\n}\n\n\u002F\u002F ✓ Good — propagates the caller's context\nfunc (s *OrderService) Create(ctx context.Context, order Order) error {\n    _, err := s.db.ExecContext(ctx, \"INSERT INTO orders ...\", order.ID)\n    return err\n}\n```\n\n## Deep Dives\n\n- **[Cancellation, Timeouts & Deadlines](.\u002Freferences\u002Fcancellation.md)** — How cancellation propagates: `WithCancel` for manual cancellation, `WithTimeout` for automatic cancellation after a duration, `WithDeadline` for absolute time deadlines. Patterns for listening (`\u003C-ctx.Done()`) in concurrent code, `AfterFunc` callbacks, and `WithoutCancel` for operations that must outlive their parent request (e.g., audit logs).\n\n- **[Context Values & Cross-Service Tracing](.\u002Freferences\u002Fvalues-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\u002Funmarshaling context across service boundaries.\n\n- **[Context in HTTP Servers & Service Calls](.\u002Freferences\u002Fhttp-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.\n\n## Cross-References\n\n- → See the `samber\u002Fcc-skills-golang@golang-concurrency` skill for goroutine cancellation patterns using context\n- → See the `samber\u002Fcc-skills-golang@golang-database` skill for context-aware database operations (QueryContext, ExecContext)\n- → See the `samber\u002Fcc-skills-golang@golang-observability` skill for trace context propagation with OpenTelemetry\n- → See the `samber\u002Fcc-skills-golang@golang-design-patterns` skill for timeout and resilience patterns\n\n## Enforce with Linters\n\nMany context pitfalls are caught automatically by linters: `govet`, `staticcheck`. → See the `samber\u002Fcc-skills-golang@golang-lint` skill for configuration and usage.\n",{"name":5,"description":7},"main","45728854e223fd51b203724ca0e5be9c8f93a5a4",".cratis\u002Fai\u002Fskills\u002Fsamber-golang-context","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go","https:\u002F\u002Fwww.cratis.io\u002Ffundamentals\u002Fgo\u002F","https:\u002F\u002Fgithub.com\u002FCratis","https:\u002F\u002Fwww.cratis.io","Open-source (MIT) event sourcing and CQRS for .NET and beyond: Chronicle, Arc, Components and Screenplay.","2026-11-14 15:52:19",{"source":8,"skillName":5,"commands":34,"prompt":44,"zipAvailable":45},[35,38,41],{"tool":36,"command":37},"npx","npx skills add Cratis\u002FFundamentals.Go --skill samber-golang-context",{"tool":39,"command":40},"bunx","bunx skills add Cratis\u002FFundamentals.Go --skill samber-golang-context",{"tool":42,"command":43},"pnpm","pnpm dlx skills add Cratis\u002FFundamentals.Go --skill samber-golang-context","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-context\u002FSKILL.md",true,[47,51,57,61,65],{"path":48,"resourceType":48,"resourceTypeDesc":49,"sizeBytes":50,"sourceHtmlUrl":15},"LICENSE","许可证",1070,{"path":52,"resourceType":53,"resourceTypeDesc":54,"sizeBytes":55,"sourceHtmlUrl":56},"references\u002Fcancellation.md","REFERENCE","参考文档",6081,"https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-context\u002Freferences\u002Fcancellation.md",{"path":58,"resourceType":53,"resourceTypeDesc":54,"sizeBytes":59,"sourceHtmlUrl":60},"references\u002Fhttp-services.md",4125,"https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-context\u002Freferences\u002Fhttp-services.md",{"path":62,"resourceType":53,"resourceTypeDesc":54,"sizeBytes":63,"sourceHtmlUrl":64},"references\u002Fvalues-tracing.md",3335,"https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-context\u002Freferences\u002Fvalues-tracing.md",{"path":66,"resourceType":53,"resourceTypeDesc":54,"sizeBytes":67,"sourceHtmlUrl":68},"UPSTREAM.md",1905,"https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-context\u002FUPSTREAM.md",[70,78,86,95,102,109],{"skillNo":71,"name":72,"nameZh":6,"description":73,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":74,"sourceHtmlUrl":75,"categories":76,"keywords":77,"publishedAt":19},"01M7DNCY3BCGY3MC5BFJ9S59BF","spf13-go","Review Go library APIs, package boundaries, tests, iterators and standard-library choices. Load for broad design or idiom reviews after the repository-owned Go skills.","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fspf13-go\u002FLICENSE","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fspf13-go\u002FSKILL.md",[],[],{"skillNo":79,"name":80,"nameZh":6,"description":81,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":82,"sourceHtmlUrl":83,"categories":84,"keywords":85,"publishedAt":19},"01M7DNCSQQH7GQ03ZETFX9XHAS","samber-golang-error-handling","Review Go error creation, wrapping, inspection and logging boundaries. Load when changing caller-visible failures after go-errors-and-context.","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-error-handling\u002FLICENSE","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fsamber-golang-error-handling\u002FSKILL.md",[],[],{"skillNo":87,"name":88,"nameZh":6,"description":89,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":90,"licenseFileUrl":91,"sourceHtmlUrl":92,"categories":93,"keywords":94,"publishedAt":19},"01M7DNCGNSMHK640AK1R3J0CPD","jetbrains-go-modern-guidelines","Choose version-compatible modern Go syntax and standard-library APIs. Load when modernizing touched Go code after reading go.mod and build constraints.","Apache-2.0","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fjetbrains-go-modern-guidelines\u002FLICENSE","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fjetbrains-go-modern-guidelines\u002FSKILL.md",[],[],{"skillNo":96,"name":97,"nameZh":6,"description":98,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":99,"categories":100,"keywords":101,"publishedAt":19},"01M7DNCBYSD6FPAPWM6PPSJR96","go-testing","Write and review Go unit, contract, transport, race, fuzz, and deterministic concurrency tests. Use for new behavior, bug fixes, flaky tests, and public API parity evidence.","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fgo-testing\u002FSKILL.md",[],[],{"skillNo":103,"name":104,"nameZh":6,"description":105,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":106,"categories":107,"keywords":108,"publishedAt":19},"01M7DNC7ES2RD6WGKC94KMMXTD","go-release-modules","Review Go module dependencies, minimum versions, compatibility, semantic labels, release-action tags, and proxy discoverability. Use for release preparation, retractions, and major-version migration planning.","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fgo-release-modules\u002FSKILL.md",[],[],{"skillNo":110,"name":111,"nameZh":6,"description":112,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":113,"categories":114,"keywords":115,"publishedAt":19},"01M7DNC22JY2MFWQR4RVFH3NZA","go-library-api-design","Design or review exported Go APIs, constructors, interfaces, generics, iteration, ownership, and compatibility. Use when adding or changing public Go library surface.","https:\u002F\u002Fgithub.com\u002FCratis\u002FFundamentals.Go\u002Fblob\u002Fmain\u002F.cratis\u002Fai\u002Fskills\u002Fgo-library-api-design\u002FSKILL.md",[],[]]