Step 1 of 5 · Reading · ~1 min
Learn
Concurrency at Scale
context.Context is how Go propagates cancellation, deadlines, and request-scoped values through call chains.
Every Go function that does I/O or blocks should accept a ctx context.Context as its FIRST argument.
Creating contexts:
Patterns:
The key must be your own unexported type, not a bare string or int — two packages that both use "user_id" would silently overwrite each other's value, and go vet flags the bare-string form.
context.WithValue is for request-scoped data only — request ID, trace span, auth user. Don't use it as a function-arg replacement.
The cardinal rule: if a goroutine outlives its caller, it should accept a context and check ctx.Done(). Goroutine leaks are the #1 Go bug at scale.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…