Gin 如何只绑定/获取 URL 查询字符串参数
gin.Context
的 ShouldBindQuery()
函数只绑定 url 查询参数而忽略 post 数据。更多的内容可以阅读 Github issue #742。
package main import ( "log" "net/http" "github.com/gin-gonic/gin" ) type Person struct { Name string `form:"name"` Address string `form:"address"` } func main() { route := gin.Default() route.Any("/testing", startPage) route.Run(":8085") } func startPage(c *gin.Context) { var person Person if c.ShouldBindQuery(&person) == nil { log.Println("====== Only Bind By Query String ======") log.Println(person.Name) log.Println(person.Address) } c.String(http.StatusOK, "Success") }