Gin 是一个用 Golang 编写的 Web 框架。具有速度快、内存占用小等特点。
使用 Gin
- 下载并安装 Gin:
1$ go get -u github.com/gin-gonic/gin
- 引入 Gin:
1import "github.com/gin-gonic/gin"
如果需要使用诸如 http.StatusOK 之类的 HTTP 状态码常量,可以引入 net/http 包:
1import "net/http"
- 开始使用 Gin。
main.go:
1package main
2
3import "github.com/gin-gonic/gin"
4
5func main() {
6 // 获取 gin.Engine 实例
7 routers := gin.Default()
8
9 // 接收 GET /hello 请求
10 routers.GET("/hello", func(ctx *gin.Context) {
11 ctx.JSON(200, gin.H{
12 "message": "Hello World!",
13 })
14 })
15
16 // 捕获并记录错误
17 defer func() {
18 if err := recover(); err != nil {
19 logrus.Error(err)
20 }
21 }()
22
23 // 监听并在 0.0.0.0:8080 上启动服务(默认即为 8080)
24 if err := routers.Run(":8080"); err != nil {
25 panic(err)
26 }
27}
接着运行:
1$ go run main.go
或者,运行以下命令也可获取 Gin 官方代码示例:
1$ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go
在上方代码示例中,
logrus.Error(err)的作用是输出Error级日志。在使用它之前需要先获取logrus包:1$ go get -u github.com/sirupsen/logrus
Gin 请求
在如上示例中,使用 routers.GET() 创建了一个接收 GET /hello 请求的路由,并且绑定了其路由的处理函数。GET() 的定义如下:
1func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
2 return group.handle(http.MethodGet, relativePath, handlers)
3}
其中:
-
RouterGroup:即gin.RouterGroup。表示一个路由组实例。由于
gin.Engine继承了gin.RouterGroup,所以gin.Engine本质上也是一个路由组实例。1type Engine struct { 2 RouterGroup 3 4 // ... 5} -
relativePath:表示路由接收的请求相对路径。例如/hello。 -
handlers:是一系列请求处理函数HandlerFunc。在GET()中可以指定多个HandlerFunc,它们将按照指定的顺序执行。中间件(middleware)、过滤器(filter)、拦截器(interceptor)等,可以基于此进行实现。 -
IRoutes:定义了一系列路由请求方法。1type IRoutes interface { 2 Use(...HandlerFunc) IRoutes 3 4 Handle(string, string, ...HandlerFunc) IRoutes 5 Any(string, ...HandlerFunc) IRoutes 6 GET(string, ...HandlerFunc) IRoutes 7 POST(string, ...HandlerFunc) IRoutes 8 DELETE(string, ...HandlerFunc) IRoutes 9 PATCH(string, ...HandlerFunc) IRoutes 10 PUT(string, ...HandlerFunc) IRoutes 11 OPTIONS(string, ...HandlerFunc) IRoutes 12 HEAD(string, ...HandlerFunc) IRoutes 13 Match([]string, string, ...HandlerFunc) IRoutes 14 15 StaticFile(string, string) IRoutes 16 StaticFileFS(string, string, http.FileSystem) IRoutes 17 Static(string, string) IRoutes 18 StaticFS(string, http.FileSystem) IRoutes 19}通过返回
IRoutes,可以使用链式调用风格来配置路由或中间件。
Gin 除了 Get 请求之外,还有可以配置其它请求方式,它们的使用方式差距不大:
1func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
2 return group.handle(http.MethodPost, relativePath, handlers)
3}
4
5func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
6 return group.handle(http.MethodGet, relativePath, handlers)
7}
8
9func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {
10 return group.handle(http.MethodDelete, relativePath, handlers)
11}
12
13func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) IRoutes {
14 return group.handle(http.MethodPatch, relativePath, handlers)
15}
16
17func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) IRoutes {
18 return group.handle(http.MethodPut, relativePath, handlers)
19}
20
21func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes {
22 return group.handle(http.MethodOptions, relativePath, handlers)
23}
24
25func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
26 return group.handle(http.MethodHead, relativePath, handlers)
27}
评论