porsamber·samber· v1.0.0 · atualizado em 2026-04-10
80
Score
Idiomatic context.Context usage in Golang — creation, propagation, cancellation, timeouts, deadlines, context values, and cross-service tracing. Apply when working with context.Context in any Go code.
Identify the need for context: — Determine where context is needed for cancellation, timeouts, or request-scoped values.
2
Create the context: — Use `context.Background()`, `context.TODO()`, `r.Context()`, `context.WithCancel()`, or `context.WithTimeout()` as appropriate.
3
Propagate the context: — Pass the context as the first argument to all downstream functions.
4
Use context-aware functions: — Use `*Context` variants of standard library functions (e.g., `QueryContext`, `ExecContext`).
5
Handle cancellation: — Listen for cancellation signals using `<-ctx.Done()` and clean up resources.
6
Defer cancellation: — If using `context.WithCancel()`, `context.WithTimeout()`, or `context.WithDeadline()`, defer the `cancel()` function immediately after creation.
7
Consider context values: — If necessary, store request-scoped metadata in context values using unexported key types.
use context in Gopropagate context in Golanghandle cancellation in Goimplement timeouts in Gopass request values in Gouse context.Context in Gocreate context in Gopropagate cancellation signals in Go