GinAppendValues 向当前请求上下文追加键值,提供类似 gin.Context Set/Get 的可变语义。 同一请求内,已持有的上下文引用会同步看到后续更新。
(c *gin.Context, keyAndValue ...any)
| 135 | // GinAppendValues 向当前请求上下文追加键值,提供类似 gin.Context Set/Get 的可变语义。 |
| 136 | // 同一请求内,已持有的上下文引用会同步看到后续更新。 |
| 137 | func GinAppendValues(c *gin.Context, keyAndValue ...any) { |
| 138 | ctx := c.Request.Context() |
| 139 | if r, ok := ctx.(*requestContext); ok { |
| 140 | r.Context = ContentWithValues(r.Context, keyAndValue...) |
| 141 | return |
| 142 | } |
| 143 | c.Request = c.Request.WithContext( |
| 144 | &requestContext{ContentWithValues(ctx, keyAndValue...)}, |
| 145 | ) |
| 146 | } |
| 147 | |
| 148 | func ContentWithValues(ctx context.Context, keyAndValue ...any) context.Context { |
| 149 | if len(keyAndValue) < 1 || len(keyAndValue)%2 != 0 { |