Use context.WithValue to attach request-scoped data to a context, but only when passing data through goroutine boundaries where no other mechanism exists. Never use it for passing large objects, sensitive data, or data that can be passed as explicit function arguments.
ctx := context.WithValue(parentCtx, key, value)
// Use ctx in child goroutines or functions
// Always cancel the context when done
defer cancel()
When NOT to use it:
- Passing data that can be an explicit argument
- Storing large objects (causes memory leaks)
- Storing sensitive data (passwords, tokens) unless encrypted
- Using
stringkeys (usestruct{}to avoid collisions)