---
source: https://iinventedlight.com/library/notes/go-context.md
title: "Go context: cancellation, deadlines and request values"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (go.dev text); prose restates cited source lines"
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Go Authors (go.dev), Go Concurrency Patterns: Context - The Go Programming Language, CC BY 4.0 (go.dev text); The Go Authors (go.dev), Contexts and structs - The Go Programming Language, CC BY 4.0 (go.dev text)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: Go"
tags: go, golang, context, concurrency
sources:
  - id: S1
    title: "Go Concurrency Patterns: Context - The Go Programming Language"
    publisher: The Go Authors (go.dev)
    url: https://go.dev/blog/context
    accessed: 2026-11-04
    license: "CC BY 4.0 (go.dev text)"
  - id: S2
    title: "Contexts and structs - The Go Programming Language"
    publisher: The Go Authors (go.dev)
    url: https://go.dev/blog/context-and-structs
    accessed: 2026-11-04
    license: "CC BY 4.0 (go.dev text)"
---
# Go context: cancellation, deadlines and request values

## Short answer
A `context.Context` carries a deadline, a cancellation signal and request-scoped values across API boundaries and goroutines. Pass it as the first argument (`ctx`) to every function that needs it; do not store it in a struct. [S1][S2]

```go
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
select { case <-ctx.Done(): return ctx.Err(); case r := <-results: return r }
```

## Key points
- `Done()` returns a channel closed when the context is canceled. [S1]
- When a request is canceled or times out, all goroutines working on it should stop. [S1]

## Claims
- id: C1
  claim: A Context carries a deadline, cancellation signal, and request-scoped values.
  source: S1
  evidence: "A Context carries a deadline, cancellation signal, and request-scoped values"
- id: C2
  claim: Done returns a channel closed when the Context is canceled.
  source: S1
  evidence: "Done returns a channel that is closed when this Context is canceled"
- id: C3
  claim: Contexts should not be stored inside a struct type.
  source: S2
  evidence: "Contexts should not be stored inside a struct type"
- id: C4
  claim: The first argument is often context.Context.
  source: S2
  evidence: "the first argument to functions and methods is often context.Context"

## Sources
- [S1] Go Concurrency Patterns: Context - The Go Programming Language. The Go Authors (go.dev). https://go.dev/blog/context . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.
- [S2] Contexts and structs - The Go Programming Language. The Go Authors (go.dev). https://go.dev/blog/context-and-structs . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
