Recover Middleware Example

February 12, 2026 ยท View on GitHub

Github StackBlitz

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:

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/recover
    
  2. Install dependencies:

    go get
    

Running the Application

  1. 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/v3"
    "github.com/gofiber/fiber/v3/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")
}

References