function call stacklast-in-first-out feature to cleverly complete the post-processing operations completed by the middleware in the custom processing function.
Django's processing method is to define a class, define a method for processing before request processing, and define a method for processing after request processing. gin's way is more flexible, but django's way is clearer.Request parameter binding
For obtaining request content, in model binding, there are the following scenarios- Binding failure is Should the user handle it himself or should the framework handle it uniformly?
- Does the user need to care about the content of the request and choose a different binder?
// 自动更加请求头选择不同的绑定器对象进行处理 func (c *Context) Bind(obj interface{}) error { b := binding.Default(c.Request.Method, c.ContentType()) return c.MustBindWith(obj, b) } // 绑定失败后,框架会进行统一的处理 func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) { if err = c.ShouldBindWith(obj, b); err != nil { c.AbortWithError(400, err).SetType(ErrorTypeBind) } return } // 用户可以自行选择绑定器,自行对出错处理。自行选择绑定器,这也意味着用户可以自己实现绑定器。 // 例如:嫌弃默认的json处理是用官方的json处理包,嫌弃它慢,可以自己实现Binding接口 func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error { return b.Bind(c.Request, obj) }