Gin开篇
Gin是什么?
Gin is a web framework written in Go (Golang). It features a martini-like API with performance that is up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love Gin.
在Github上它的自我介绍可以看出,这是一个主打性能的Web框架。
Gin的起点-Engine
我们可以在官方网站的demo示例看出,Gin的使用需要最基础的Engine结构体,所以我们将从Engine入手,逐步分析它的处理逻辑。
以下是Engine对象的成员:
// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
// Create an instance of Engine, by using New() or Default()
type Engine struct {
RouterGroup
RedirectTrailingSlash bool
RedirectFixedPath bool
HandleMethodNotAllowed bool
ForwardedByClientIP bool
AppEngine bool
UseRawPath bool
UnescapePathValues bool
RemoveExtraSlash bool
RemoteIPHeaders []string
TrustedPlatform string
MaxMultipartMemory int64
UseH2C bool
delims render.Delims
secureJSONPrefix string
HTMLRender render.HTMLRender
FuncMap template.FuncMap
allNoRoute HandlersChain
allNoMethod HandlersChain
noRoute HandlersChain
noMethod HandlersChain
pool sync.Pool
trees methodTrees
maxParams uint16
maxSections uint16
trustedProxies []string
trustedCIDRs []*net.IPNet
}
从该结构体的注释中可以看出,我们可以通过New和Default方法来创建Engine对象,从源码看出两者的区别是Default会默认帮我们嵌入两个middleware 分别是Logger和Recovery。