Recover 中间件示例
本项目演示了如何使用 Fiber 框架的 Recover
中间件在 Go 应用中实现恢复机制。
先决条件
确保已安装以下项:
- Golang
- Fiber 包
设置
-
克隆仓库
git clone https://github.com/gofiber/recipes.git
cd recipes/recover -
安装依赖
go get
运行应用
- 启动应用
go run main.go
示例
以下是在 Fiber 应用中设置 Recover
中间件的示例:
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
)
func main() {
app := fiber.New()
// Use the Recover middleware
app.Use(recover.New())
app.Get("/", func(c *fiber.Ctx) error {
// This will cause a panic
panic("something went wrong")
})
app.Listen(":3000")
}