Gin 框架:*gin.Engine 主要方法

gin.Engin

在 Gin 框架中,*gin.Engine 是整个 Web 应用的核心引擎 ,也是调用 gin.Default()gin.New() 时返回的实例。它不仅是路由组(RouterGroup) ,还集成了HTTP 服务启动、全局中间件管理、错误处理、静态文件服务、模板渲染等关键能力。

所有 Gin 应用的生命周期------从路由注册到服务启动------都围绕 gin.Engine 展开。理解它的主要方法,是掌握 Gin 高效开发的关键第一步。

复制代码
gin.Engine
├── RouterGroup (路由组,包含所有路由方法)
│   ├── GET/POST/PUT/DELETE 等路由方法
│   ├── Group() 路由分组
│   └── Use() 中间件
├── HTTP 服务功能
│   ├── Run() 启动服务器
│   └── RunTLS() HTTPS 服务
├── 中间件管理
│   └── Use() 添加全局中间件
└── 错误处理
    ├── NoRoute() 404 处理
    └── NoMethod() 405 处理

路由相关(继承自 RouterGroup):

go 复制代码
// HTTP 方法

GET(path string, handlers ...HandlerFunc) IRoutes

POST(path string, handlers ...HandlerFunc) IRoutes

PUT(path string, handlers ...HandlerFunc) IRoutes

DELETE(path string, handlers ...HandlerFunc) IRoutes

PATCH(path string, handlers ...HandlerFunc) IRoutes

HEAD(path string, handlers ...HandlerFunc) IRoutes

OPTIONS(path string, handlers ...HandlerFunc) IRoutes

ANY(path string, handlers ...HandlerFunc) IRoutes

中间件和分组(继承自 RouterGroup):

go 复制代码
Use(middleware ...HandlerFunc) IRoutes

Group(component string, handlers ...HandlerFunc) *RouterGroup

静态文件(继承自 RouterGroup):

go 复制代码
StaticFile(relativePath, filepath string) IRoutes

Static(relativePath, root string) IRoutes

StaticFS(relativePath string, fs http.FileSystem) IRoutes

其他方法

go 复制代码
Run(addr ...string) error

RunTLS(addr, certFile, keyFile string) error

RunUnix(file string) error

RunListener(listener net.Listener) error

  

NoRoute(handlers ...HandlerFunc)

NoMethod(handlers ...HandlerFunc)

  

LoadHTMLGlob(pattern string)

LoadHTMLFiles(files ...string)

SetHTMLTemplate(templ *template.Template)

SetFuncMap(funcMap template.FuncMap)

  

Routes() (routes RoutesInfo)

HandleMethodNotAllowed bool
相关推荐
忧郁的Mr.Li20 小时前
SpringBoot中实现多数据源配置
java·spring boot·后端
暮色妖娆丶21 小时前
SpringBoot 启动流程源码分析 ~ 它其实不复杂
spring boot·后端·spring
Coder_Boy_21 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例中相关概念
java·人工智能·spring boot·后端·spring
Java后端的Ai之路1 天前
【Spring全家桶】-一文弄懂Spring Cloud Gateway
java·后端·spring cloud·gateway
野犬寒鸦1 天前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习
Honmaple1 天前
OpenClaw 实战经验总结
后端
golang学习记1 天前
Go 嵌入结构体方法访问全解析:从基础到进阶陷阱
后端
NAGNIP1 天前
程序员效率翻倍的快捷键大全!
前端·后端·程序员
qq_256247051 天前
从“人工智障”到“神经网络”:一口气看懂 AI 的核心原理
后端
无心水1 天前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf