Golang笔记|gin 解决get请求中shouldBindQuery参数绑定不到的问题

2,023 阅读1分钟

参数结构体

type TreeMenuReq struct {
   Type      int64  `json:"type"` // 菜单类型
   QueryType string `json:"queryType"`
}

接口接口参数

func TreeMenuList(c *gin.Context) {
   var req dto.TreeMenuReq
   if err := c.ShouldBindQuery(&req); err != nil {
      response.Fail(c, "参数绑定失败")
      return
   }
   menu, err := menuService.GetTreeList(&req)
   if err != nil {
      response.Fail(c, err.Error())
      return
   }
   response.Success(c, menu)
}

该请求为get请求,接收参数,使用一键绑定函数sholdBindQuery,但是出现了绑定不到的问题,req中的参数为默认的初始值

解决方式:

在上方的参数结构体中 参数后面加上form:xxx 即可解决绑定问题

type TreeMenuReq struct {
   Type      int64  `form:"type" json:"type"` // 菜单类型
   QueryType string `form:"queryType" json:"queryType"`
}