在 gin 里面,路由可以通过 r := gin.Default() 返回的对象来定义,这个方法会返回框架实例,而框架实例,也就是 gin.Engine 的实例,嵌套了 RouterGroup 结构体,因此可以直接通过 r 来定义路由,比如:
r.GET("/test", func(c *gin.Context) {
c.String(200, "Hello world!")
})
中间件
在 gin 里面,中间件的定义都是通过 RouterGroup 里面的 Use 方法来定义的。
中间件的定义方式
- 使用
engine.Use()的时候,定义的中间件是对所有请求都有效的。 - 使用
g := r.Group("/user"); g.Use()的时候,定义的中间件只对/user路由分组有效
针对路由分组的中间件
// 不在 /user 路由分组下
r.GET("/test", func(c *gin.Context) {
c.String(200, "test...")
})
// /user 路由分组
g := r.Group("/user")
g.Use(func(c *gin.Context) {
fmt.Println("inner group")
})
g.GET("/", func(c *gin.Context) {
c.String(200, "user...")
})
g.GET("/test", func(c *gin.Context) {
c.String(200, "user test...")
})
// GET http://localhost:8085/test => test...(控制台无输出)
// GET http://localhost:8085/user => user...(同时控制台输出 “inner group”)
// GET http://localhost:8085/user/test => user test...(同时控制台输出 “inner group”)
gin 中路由的结构
在 gin 里面,路由实际上是一棵前缀树,树的节点保存在 Engine.trees 属性里面。
假如我们定义了如下路由,
r.GET("/test", func(c *gin.Context) {})
r.GET("/testa", func(c *gin.Context) {})
r.GET("/testb", func(c *gin.Context) {})
那么这棵树大概长以下这样:

- 对于每一个 HTTP 请求方法,在 gin 的路由树里面第一层有一个节点,这个节点的类型是
tree - 路由树第一层节点是
tree - 第二层以及更高层的节点是
node
tree 和 node 的定义如下:
type methodTree struct {
method string
root *node
}
type node struct {
path string
indices string
wildChild bool
nType nodeType
priority uint32
children []*node // child nodes, at most 1 :param style node at the end of the array
handlers HandlersChain
fullPath string
}
RouterGroup 思维导图

本文详细介绍了在Gin框架中如何定义路由和中间件,包括全局和路由分组的使用,以及路由结构的实现,以树状结构为例,展示了如何组织和处理HTTP请求.
6万+

被折叠的 条评论
为什么被折叠?



