What are the advantages of the gin framework?

藏色散人
Release: 2020-09-16 09:38:07
forward
5794 people have browsed it

The following column golang tutorial will introduce you to the advantages of the gin framework. I hope it will be helpful to friends in need!

What are the advantages of the gin framework?

##Comparison between gin and django

Middleware

Using the

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?
The answer given in the gin framework for these scenarios is: Provide different methods to meet the above needs. The key point here still lies in the usage scenario.

// 自动更加请求头选择不同的绑定器对象进行处理
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)
}
Copy after login

The above is the detailed content of What are the advantages of the gin framework?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
gin
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!