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

Taught in

References