Recover Middleware Example
This project demonstrates how to implement a recovery mechanism in a Go application using the Fiber framework's Recover
middleware.
Prerequisites
Ensure you have the following installed:
- Golang
- Fiber package
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git
cd recipes/recover -
Install dependencies:
go get
Running the Application
- Start the application:
go run main.go
Example
Here is an example of how to set up the Recover
middleware in a Fiber application:
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")
}