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
相关推荐
小码哥_常2 小时前
别再被误导!try...catch性能大揭秘
后端
苍何4 小时前
30分钟用 Agent 搓出一家跨境网店,疯了
后端
ssshooter5 小时前
Tauri 2 iOS 开发避坑指南:文件保存、Dialog 和 Documents 目录的那些坑
前端·后端·ios
追逐时光者5 小时前
一个基于 .NET Core + Vue3 构建的开源全栈平台 Admin 系统
后端·.net
程序员飞哥5 小时前
90后大龄程序员失业4个月终于上岸了
后端·面试·程序员
GetcharZp7 小时前
Git 命令行太痛苦?这款 75k Star 的神级工具,让你告别“合并冲突”恐惧症!
后端
Victor3568 小时前
MongoDB(69)如何进行增量备份?
后端
Victor3568 小时前
MongoDB(70)如何使用副本集进行备份?
后端
千寻girling8 小时前
面试官 : “ 说一下 Python 中的常用的 字符串和数组 的 方法有哪些 ? ”
人工智能·后端·python
ywf12159 小时前
Spring Boot接收参数的19种方式
java·spring boot·后端