Go语言之gin框架源码学习

109 阅读2分钟

Go语言之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

路由源码解析:

go_gin源码解析.drawio.png

中间件源码解析:

gin_middleware.png

gin 源码图片解析:

default_canvas.png