深入解析Go语言Gin框架:路由注册与中间件源码剖析

深入解析Go语言Gin框架:路由注册与中间件源码剖析Introduction:本篇文章将带你深入探索Gin框架的源码,重点剖析路由注册、路由匹配机制以及中间件的执行流程。通过阅读源码并结合实际示例,你将更好地理解Gin框架的高效性和灵活性,掌握其核心工作原理。这篇文章适合那些想要深入学习Go语言

深入解析Go语言Gin框架:路由注册与中间件源码剖析

Introduction:

本篇文章将带你深入探索Gin框架的源码,重点剖析路由注册、路由匹配机制以及中间件的执行流程。通过阅读源码并结合实际示例,你将更好地理解Gin框架的高效性和灵活性,掌握其核心工作原理。这篇文章适合那些想要深入学习Go语言及其常用Web框架的开发者,帮助你在实际项目中更好地应用Gin。

gin 框架路由注册与路由匹配、中间件

package main

import (
 "fmt"
 "github.com/gin-gonic/gin"
 "net/http"
)

func func1(c *gin.Context) {
 fmt.Println("func1")
}
func func2(c *gin.Context) {
 fmt.Println("func2 before")
 c.Next()
 fmt.Println("func2 after")
}
func func3(c *gin.Context) {
 fmt.Println("func3")
 //c.Abort()
}
func func4(c *gin.Context) {
 fmt.Println("func4")
 c.Set("name", "test")
}
func func5(c *gin.Context) {
 fmt.Println("func5")
 v, ok := c.Get("name")
 if ok {
  vStr := v.(string) // 类型转换
  fmt.Println("vStr", vStr)
 }
}

func main() {
 r := gin.Default()

 r.GET("/hello", func(c *gin.Context) {
  c.String(http.StatusOK, "ok")
 })

 shopGroup := r.Group("/shop", func1, func2) // 针对当前路由组生效的中间件
 shopGroup.Use(func3)
 {
  shopGroup.GET("/index", func4, func5)
 }

 r.Run()
}

运行

Code/go/gin_demo via 🐹 v1.20.3 via 🅒 base 
➜ go run main.go 
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /hello                    --> main.main.func1 (3 handlers)
[GIN-debug] GET    /shop/index               --> main.func5 (7 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
func1
func2 before
func3
func4
func5
func2 after
[GIN] 2023/06/10 - 18:14:44 | 200 |      24.167µs |             ::1 | GET      "/shop/index"
[GIN] 2023/06/10 - 18:14:44 | 404 |       1.292µs |             ::1 | GET      "/favicon.ico"
^Csignal: interrupt

Code/go/gin_demo via 🐹 v1.20.3 via 🅒 base took 2m 12.5s 
➜ go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /hello                    --> main.main.func1 (3 handlers)
[GIN-debug] GET    /shop/index               --> main.func5 (7 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
func1
func2 before
func3
func2 after
[GIN] 2023/06/10 - 18:16:06 | 200 |      46.792µs |             ::1 | GET      "/shop/index"
^Csignal: interrupt

Code/go/gin_demo via 🐹 v1.20.3 via 🅒 base took 5m 4.4s 
➜ go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /hello                    --> main.main.func1 (3 handlers)
[GIN-debug] GET    /shop/index               --> main.func5 (7 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
func1
func2 before
func3
func2 after
[GIN] 2023/06/10 - 18:21:12 | 200 |      44.917µs |             ::1 | GET      "/shop/index"
^Csignal: interrupt

Code/go/gin_demo via 🐹 v1.20.3 via 🅒 base took 22.0s 
➜ go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /hello                    --> main.main.func1 (3 handlers)
[GIN-debug] GET    /shop/index               --> main.func5 (7 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
func1
func2 before
func3
func4
func5
vStr test
func2 after
[GIN] 2023/06/10 - 18:21:34 | 200 |      55.666µs |             ::1 | GET      "/shop/index"

访问:<http://localhost:8080/shop/index>

路由源码解析

中间件源码解析

gin 源码图片解析

参考

点赞 0
收藏 1
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
寻月隐君
寻月隐君
0x750E...B6f5
不要放弃,如果你喜欢这件事,就不要放弃。如果你不喜欢,那这也不好,因为一个人不应该做自己不喜欢的事。