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
相关推荐
程序员老赵4 小时前
Docker 部署 Rocky Linux:轻松搭建 RHEL 兼容企业级基础镜像平台
linux·后端·docker
用户6152612132104 小时前
Java主流框架与源码:Spring Framework
后端
Casbin开源社区4 小时前
一个面板管住 29 个 AI 编程 Agent:Casbin Gateway 的配置统一、协议互转、用量统计与权限管控
人工智能·golang·开源·gateway·casbin
yume_sibai5 小时前
03-Rust 函数式编程特性(闭包 + Iterator + Option/Result + 链式调用)
开发语言·后端·rust
吃饱了得干活5 小时前
Redis 不是死脑筋,它是一套“会进化”的存储系统
redis·后端
伩仁5 小时前
别再 HTTP 200 一把梭了:用 RFC 9457 Problem Details 给 FastAPI 错误响应"立规矩"
后端
MeetTanG5 小时前
Go 实战锦囊|errgroup:优雅地管理并发任务组
后端·go
PragmaticWorks5 小时前
DDD 学了很多却用不上?因为你把“责任”和“时机”揉在了一起
后端·领域驱动设计
凯哥Java6 小时前
System.setProperty 的正确姿势:Spring Boot 启动类里的“缺省值“魔法
java·spring boot·后端
柯克七七6 小时前
Vue刷新页面数据重置?一招解决Vuex状态丢失
后端