gin02:gin路径中的参数

本文来探讨gin是如何获取路径中的参数

gin获取路径中参数的示例

go 复制代码
func main() {
    router := gin.Default()

    router.GET("/user/:name", func(c *gin.Context) {
       name := c.Param("name")
       c.String(http.StatusOK, "hello %s", name)
    })
    router.GET("/user/:name/*action", func(context *gin.Context) {
       name := context.Param("name")
       action := context.Param("action")
       meesage := name + " is " + action
       context.String(http.StatusOK, meesage)
    })
    router.POST("/user/:name/*action", func(context *gin.Context) {
       b := context.FullPath() == "/user/:name/*action"
       context.String(http.StatusOK, "%t", b)
    })

    router.GET("/user/groups", func(context *gin.Context) {
       context.String(http.StatusOK, "可用的用户组是[...]")
    })
    router.Run("localhost:9090")
}

gin能够通过context.Param()就非常简单的获取了路径中的参数,接下来探讨它是怎么做到的。

Params

gin.Context的结构体中有一个Params字段,其实际是[] Param类型

bash 复制代码
type Params []Param

Param的结构体十分简单

go 复制代码
type Param struct {
    Key   string
    Value string
}

这就是底层参数的存储的数据结构,在调用context.Param()获取参数的时

go 复制代码
func (c *Context) Param(key string) string {
    return c.Params.ByName(key)
}

实际是调用了c.Params.ByName()

go 复制代码
func (ps Params) ByName(name string) (va string) {
    va, _ = ps.Get(name)
    return
}

而其底层是通过Get获取value

go 复制代码
func (ps Params) Get(name string) (string, bool) {
    for _, entry := range ps {
       if entry.Key == name {
          return entry.Value, true
       }
    }
    return "", false
}

这里可以看出,实际获取参数的操作,就是循环遍历了一遍底层的Param切片。默认返回空字符串""

Params的生命周期

在探讨了底层是如何获得参数的问题之后,现在有一个新的问题,Params的生命周期历程。 1.Params的重置

  • EngineServeHTTP方法中,从Context池中创建或者拿出一个Context
  • 调用Context.reset()针对它进行初始化
ini 复制代码
func (c *Context) reset() {
    c.Writer = &c.writermem
    c.Params = c.Params[:0]
    c.handlers = nil
    c.index = -1

    c.fullPath = ""
    c.Keys = nil
    c.Errors = c.Errors[:0]
    c.Accepted = nil
    c.queryCache = nil
    c.formCache = nil
    c.sameSite = 0
    *c.params = (*c.params)[:0]
    *c.skippedNodes = (*c.skippedNodes)[:0]
}
  1. Params的设置
    • 在根据请求方法获取到对应的路由树之后
    • 调用root.getValue(),传入请求的路径,和c.params
    • 获得一个nodeValue对象,其中包括逻辑处理链参数注册的路由
    • nodeValueparams赋给Context.Params
csharp 复制代码
root := t[i].root
// Find route in tree
value := root.getValue(rPath, c.params, c.skippedNodes, unescape)
if value.params != nil {
    c.Params = *value.params
}
  1. Context被回收
相关推荐
我不是外星人1 小时前
有了 Harness Engineering ,真的还需要研发工程师吗?
前端·后端·ai编程
candyTong1 小时前
RTK 技术原理:一次典型会话里,80% 上下文是怎么省下来的
javascript·后端·架构
Rust研习社3 小时前
组合真的优于继承吗?为什么 Rust 和 Go 都拥抱组合舍弃继承?
后端·rust·编程语言
IT_陈寒4 小时前
JavaScript的闭包把我坑惨了,说好的内存会自动回收呢?
前端·人工智能·后端
CaffeinePro4 小时前
Pydantic深度使用:数据校验、枚举、ORM映射
后端·fastapi
Chenyiax5 小时前
从 Chat 到 Responses:OpenAI API 抽象为什么变了?
后端
MariaH5 小时前
Koa和Express的区别
后端
MariaH5 小时前
Koa框架的使用
后端
luckdewei6 小时前
那个用 passlib 做认证的新同事,上线第一天就把用户密码写进了日志
后端
ping某7 小时前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx