Junior — Middle
What information is permissible to transmit through context in an application?
sobes.tech AI
Answer from AI
In Go, information related to request or operation lifecycle management is usually passed through the context (context.Context), for example:
- Canceling an operation (via the
Done()channel). - Deadlines and timeouts (via
Deadline()). - Values needed throughout the call chain, such as request identifiers, authentication tokens, metadata.
Important: Large objects, state, or data not related to request management should not be placed in the context to avoid violating its purpose and causing memory leaks.
Example of passing a value through the context:
ctx := context.WithValue(context.Background(), "requestID", "12345")
requestID := ctx.Value("requestID")