1 回答
TA贡献1801条经验 获得超8个赞
你可以使用Context这种东西,这里是一个例子:
package main
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
type ParamsOne struct {
Username string `json:"username"`
}
type ParamsTwo struct {
Username string `json:"username"`
}
func first(c *gin.Context) {
c.Set("variable", "foo")
c.Next()
}
func second(c *gin.Context) {
v := c.MustGet("variable")
log.Printf("in middleware %s", v)
c.Next()
}
func main() {
r := gin.New()
r.Use(first)
r.Use(second)
r.POST("/", func(c *gin.Context) {
var f ParamsOne
if err := c.ShouldBindBodyWith(&f, binding.JSON); err != nil {
log.Printf("%+v", err)
}
log.Printf("%+v", f)
var ff ParamsTwo
if err := c.ShouldBindBodyWith(&ff, binding.JSON); err != nil {
log.Printf("%+v", err)
}
log.Printf("%+v", ff)
v := c.MustGet("variable")
log.Printf("in route %s", v)
c.IndentedJSON(http.StatusOK, f)
})
r.Run(":4000")
}
输出:
example$ ./example
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] POST / --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :4000
2020/07/05 13:11:06 in middleware foo
2020/07/05 13:11:06 {Username:somename}
2020/07/05 13:11:06 {Username:somename}
2020/07/05 13:11:06 in route foo
- 1 回答
- 0 关注
- 190 浏览
添加回答
举报