[英]gin golang: what is gin.Context.Keys
I was tring to use the method context.GetBool
( here ) from the go-gin framework with some query params.我试图使用go-gin框架中的方法context.GetBool
( here ) 和一些查询参数。 It doesn't work as excepted and I think the Context.Keys
is not populated by query params.它不能作为例外工作,我认为Context.Keys
不是由查询参数填充的。
So my question is: what is gin.Context.Keys
, and how should it be populated when making a request?所以我的问题是:什么是gin.Context.Keys
,以及在发出请求时应该如何填充它?
PS: the question was already asked here , but left without a proper answer. PS:这个问题已经在这里问过了,但是没有正确的答案。
tl;dr The Keys
field is what backs up Gin's Context implementation of context.Context
interface as a request-scoped key/value carrier. tl;dr Keys
字段支持 Gin 的context.Context
接口实现作为请求范围的键/值载体。
I think the Context.Keys is not populated by query params.我认为 Context.Keys 不是由查询参数填充的。
Correct.正确的。 Context.Keys
is unrelated to query params. Context.Keys
与查询参数无关。 Query params are available withContext.Query
.查询参数可用于Context.Query
。
About Keys
instead, the document on the struct field reads:相反,关于Keys
,结构字段上的文档显示:
Keys is a key/value pair exclusively for the context of each request . Keys 是专门用于每个请求上下文的键/值对。
And these key/value pairs are accessible with Get
and Set
.这些键/值对可以通过Get
和Set
访问。 The docs of this latter one are:后一个的文档是:
Set is used to store a new key/value pair exclusively for this context. Set 用于专门为此上下文存储新的键/值对。 It also lazy initializes c.Keys if it was not used previously.如果之前没有使用它,它还会延迟初始化 c.Keys。
Hence the field is similar to context
package's Context.WithValue
and Context.Value
, eg request-scoped parameters .因此该字段类似于context
包的Context.WithValue
和Context.Value
,例如请求范围的参数。 Gin's Context Keys
is the exported map that stores the raw key/value pairs. Gin 的上下文Keys
是存储原始键/值对的导出的 map。 The methods such as GetBool
are convenience, in that you don't have to type-assert the interface{}
values yourself. GetBool
等方法很方便,因为您不必自己对interface{}
值进行类型断言。
Unlike other web frameworks, Gin's Context does not wrap a context.Context
value (except for c.Request.Context
), instead it directly implements the interface.与其他 web 框架不同,Gin 的 Context 不包装context.Context
值( c.Request.Context
除外),而是直接实现接口。 This includes theValue
method itself, which also accesses the underlying Keys
field.这包括Value
方法本身,它也访问底层的Keys
字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.