Go 语言

2025年04月19日更新 4 人订阅
原价: ¥ 2 限时优惠
专栏简介 数据结构 in Golang:Hash Tables(哈希表) 算法 in Golang:Quicksort(快速排序) 算法 in Golang:Recursion(递归) 用Go语言构建分布式系统:服务注册、发现与日志管理实践 算法 in Golang:Selection sort(选择排序) Go语言之基本数据类型 深入探索Go语言:从初识到实践 实战:Go语言项目之使用JWT实现用户认证 算法 in Go:Binary Search(二分查找) 算法 in Golang:Breadth-first search(BFS、广度优先搜索) 算法 in Golang:D & C(分而治之) Go语言(Golang)编写最简单的命令行工具 深入探讨 Go 语言中的自定义 Zap 日志 Go语言详解:实现MySQL数据库的增删改查操作 深入解析Go语言Gin框架:路由注册与中间件源码剖析 Go 语言之在 Gin 框架中使用 Zap 实现高效日志管理 Go 语言中 zap 日志库的高效使用指南 Go 语言日志系统自定义:精细化日志管理与应用示例 Go 语言之搭建通用 Web 项目开发脚手架 Go语言结构体(struct)详解:定义、使用与JSON编码 探索 Go 语言的无类设计:从 Struct 到组合的优雅之道 地鼠工厂的秘密:解锁Go语言中goroutine的并发魔法 Go 并发编程实战:从互斥锁到 Goroutine 的优雅之道 用 Go 语言打造高效 TCP 扫描器:从入门到并发优化 gogen:一键生成 Go 项目,开发者的效率利器 深入剖析 Go 接口底层实现:从 eface 到 iface(基于 Go 1.24 源码) Go并发实战:5协程随机数求和 Go 开发必备:解锁 Viper 配置管理的正确姿势

深入解析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 条评论

请先 登录 后评论