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
相关推荐
ltl7 小时前
Transformer 整体架构:一张图看懂
后端
ltl7 小时前
Decoder 详解:为什么它天生适合生成
后端
ltl8 小时前
Encoder 详解:6 层堆叠到底在做什么
后端
程序员cxuan8 小时前
微信读书官方发了 skills,把我给秀麻了。
人工智能·后端·程序员
存在morning8 小时前
【GO语言开发实践】二 GO 并发快速上手
大数据·开发语言·golang
未若君雅裁8 小时前
Spring AOP、日志切面与声明式事务原理
java·后端·spring
zhangxingchao9 小时前
AI应用开发六:企业知识库
前端·人工智能·后端
红尘散仙9 小时前
一个 `#[uniffi::export]`,把 Rust 接进 React Native
前端·后端·rust
红尘散仙9 小时前
一行 `#[specta::specta]`,让 Tauri IPC 有类型
前端·后端·rust
XinZong12 小时前
OpenClaw 中最经典的 6 款skill,真正能进工作流的 skills
javascript·后端