79442735

Date: 2025-02-16 07:33:44
Score: 0.5
Natty:
Report link

You can set ContextWithFallback field to true after initializing default Gin Engine.

r := gin.Default()
r.ContextWithFallback = true

This will tell gin to extract value using gin.Context.Value() when value is not found using gin.Context.Request.Context.Value(). This way you don't need to modify your getCurrentUser function call as suggested by @mkopriva

This code here checks if gin.Engine.ContextWithFallback is enabled and gin.Request.Context() is not nil. If both the conditions are met then gin.Context.Value(key) returns gin.Context.Request.Context.Value(key).

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @mkopriva
  • Low reputation (0.5):
Posted by: Coder_H