0007: Context — Cancellation, Deadlines, and Values
Date: 2026-06-14 Status: Accepted
Insight
context.Context is Go's solution to cancellation propagation and request-scoped data. Every cloud-native Go API takes a ctx as its first parameter. It's the mechanism that makes graceful shutdown, timeout-based resource cleanup, and distributed tracing possible.
The learner now has both channels (Lesson 0005) and sync primitives (Lesson 0006). Context builds on channels — ctx.Done() returns a <-chan struct{} that closes when the context is cancelled. Understanding the channel connection makes context feel like a natural extension rather than magical behavior.
The values aspect (context.WithValue) is controversial. Kubernetes uses it for request metadata (user info, trace IDs), but overuse leads to opaque dependencies. The lesson should teach proper use: only for request-scoped data, never for optional function parameters.
Consequences
- Lesson 0007: Context creation, cancellation propagation, deadlines, values, best practices
- Should explicitly connect
ctx.Done()to the done channel pattern from Lesson 0005 - Examples should mirror real cloud-native patterns: HTTP server shutdown, controller reconciliation with timeout
- This is the last "fundamental" lesson before modules (0008), generics (0009), and reflection/unsafe (0010)