Junior — Middle
What data or objects can be stored inside the application or session context?
sobes.tech AI
Answer from AI
In the context of an application or session in Go, data that needs to persist throughout the request or user session lifecycle is usually stored. This can include:
- User information (ID, roles, settings)
- Authentication or authorization tokens
- Temporary data required for request processing
- Contextual values affecting execution logic (e.g., timeouts, deadlines)
Example of using context in Go:
func handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
userID := ctx.Value("userID")
// Use userID to process the request
}
It is important not to store large objects or data in the context that are not related to request processing to avoid memory leaks.