关于go:gin中Context中的GetQueryParam函数都是从哪里获取数据的

2次阅读

共计 2548 个字符,预计需要花费 7 分钟才能阅读完成。

大家好,我是渔夫子。

在应用 gin 框架解决一次申请的过程中,能够通过 Context 构造体提供的办法获取或设置一个指定 key 的值。在 Context 中有多个通过 key 获取值的函数:GetString(key string) (s string)Param(key string) stringQuery(key string) (value string)PostForm(key string) (value string)GetHeader(key string)Cookie(name string)等。

那么,这些函数到底是从哪里获取数据的呢?本文就带你一起来探索这些函数底层的数据源。

Context 中 Get 函数的数据源

Context.Get 函数是 从 Context.Keys 字段中获取的数据。咱们看下 Context 的 Keys 字段的定义(为不便文章阐明,咱们省略了 Context 中的其余无关字段):

type Context struct {
    // Keys is a key/value pair exclusively for the context of each request.
    Keys map[string]any
}

能够看到,Keys是一个 map[string]any 类型的 map。anygininterface{}的别名。通过该定义可知,在 Keys 字段中键必须是 string 类型,值能够是任意类型。

这个 Keys 中的值又来源于哪里呢?是在 gin 服务在解决申请时通过 Context.Set 函数设置的。Keys 里的数据的生命周期是本次申请,作用域范畴也仅限于本次申请。申请完结了,Keys 里的值也就完结了。

同时,Context.Keys字段的初始化也采纳了lazy 模式。即在应用 Context.Set 函数时才进行初始化的。

对于 Get 函数来说,还有一些 GetXXX 的辅助函数,比方:MustGet、GetString、GetBool、GetInt、GetInt64 等等,也都是 从 Keys 中获取数据。

Context 中 Param 函数的

Context.Param(key string)函数是从 正则门路中获取对应的匹配数据值。在 gin 中,正则门路的参数是被解析到 Context.Params 字段中的。其字段定义如下:

type Context struct {Params   Params}

type Params []Param

type Param struct {
    Key   string
    Value string
}

例如,咱们定义了正则门路 “/user/:id”,那么通过 c.Param(“id”)函数就能获取到本次路由中的 id 参数值。

router.GET("/user/:id", func(c *gin.Context) {
    // a GET request to /user/john
    id := c.Param("id") // id == "john"
})

Context 中 Query 函数的数据源

Context.Query 函数是获取的 url 中的 查问参数的值 。在 gin 中,将查问参数的值会解析到 Context 中的queryCache 字段中,而 queryCache 的数据则来源于 Context.Request.URL.RawQuery 中。如下:

type Context struct {// queryCache caches the query result from c.Request.URL.Query().
    queryCache url.Values
}

比方,咱们申请的 url 是 GET /path?id=1234&name=Manu&value=,那么就能够通过 Query 查问到 id、name 和 value 对应的值:

c.Query("id") == "1234"
c.Query("name") == "Manu"
c.Query("value") == ""c.Query("wtf") ==""

Context 中的 PostForm 函数的数据源

Context.PostForm(key string) 函数是从form 表单的 urlencode 编码的汇合中获取数据。这里是 form 表单中以 urlencoded 模式编码的 key/value 值。如果是上传的文件,则不能通过该函数获取。

在 gin 框架中,会把 form 表单的数据缓存到 Context 的 formCache 中。获取时,会间接从 formCache 中获取。如下:

type  Context struct {formCache url.Values}

通过 formCache 字段的类型为 url.Values 也能够晓得,该字段存储的只有 form 表单中的 urlencode 编码的 key/value 值。
比方,有如下 form 表单,那么 formCache 中的值就是 username 和 password。而 action 值中的 utm_source=login 以及 file 类型的参数是不在 formCache 中的。

<form action="http://localhost:9090/login?utm_source=login" method="POST" enctype="multipart/form-data">
    <input type="text" name="username" value="yufuzi" />
    <input type="text" name="password" />
    <input type="file" name="f" />
    <input type="submit" value="submit" />
</form>

总结

本文总结了 gin 框架中应用 Context 构造体中获取指定 key 的值的各种函数的数据起源。通过 Context 中 Keys 字段、动静路由中门路中的参数的 Params 字段、url 查问中查问参数的 queryCache 字段以及 form 表单中 urlencode 参数的 formCache 字段。具体能够参考下图:

特地举荐:一个专一 go 我的项目实战、我的项目中踩坑教训及避坑指南、各种好玩的 go 工具的公众号,「Go 学堂」,专一实用性,十分值得大家关注。点击下方公众号卡片,间接关注。关注送《100 个 go 常见的谬误》pdf 文档。

原文链接:https://mp.weixin.qq.com/s/amfGIdxA166cqKzpGd_Dpw

正文完
 0