context value
Data attached to a context with context.WithValue and read back with ctx.Value, meant for request-scoped details such as a trace label.
In more detail
Values travel with the request through every layer without extra parameters. They are untyped and invisible in function signatures, so do not use them for configuration or optional arguments. Use a private key type instead of a plain string to avoid clashes between packages.
Example
ctx := context.WithValue(context.Background(), "request", "probe")Related terms
- context — The Go standard library package whose Context value carries a deadline, a cancellation signal and request-scoped values through a chain of function calls.
- metadata — Data that describes other data, such as a file name, a creation time or a package version.
Taught in
- The context Package — Concurrent Systems Programming with Go