How to correctly use package context
summary
Go 1.7 context provides request-wide cancellation and scoped values; use context.Background at RPC entry, pass Context as the first arg or on a request struct, and prefer errgroup.WithContext for parallel RPCs so the first error cancels the rest. Always call the cancel func to avoid timer leaks. Store only immutable request-scoped metadata, scope keys with a private type, and let values inform not control logic; follow always call cancel and use error group for fan‑out.
AI-written summary · about
chapters (32)
- 0:00Introduction
- 0:55Outline
- 1:25Why does context exist
- 2:19Multiple RPC calls
- 4:24The bigger problem
- 4:43Threadlocal variables
- 5:08Tree
- 5:23Context chain
- 6:12API
- 6:51When to use context
- 7:42How to create context
- 8:19How to integrate context into your API
- 9:37Where to put context
- 10:55Closing context
- 12:37Error groups
- 15:49Scoped values
- 17:17Scope keyspace
- 18:27Immutability
- 19:18Context Values
- 20:27Problem with Context Values
- 21:15Summary of Code
- 21:37Function Signature
- 22:28Refactoring
- 23:10Request ID
- 24:26Database Connection
- 25:00Creative Debugging
- 25:17CV Trace
- 26:32Requests of Value
- 27:30Abuse of Context
- 28:46Summary of Context
- 29:26Summary of Operations
- 30:36Questions








