How do I build a REST API with Fiber in Go?

Building a REST API with Fiber in Go is a straightforward process that allows developers to create efficient, high-performance applications. Fiber is an Express.js inspired web framework for Go, which makes building web applications and APIs a breeze. Below is a simple example of how to set up a REST API using Fiber.

Follow the example below to create an API with CRUD (Create, Read, Update, Delete) functionalities.

package main import ( "github.com/gofiber/fiber/v2" ) func main() { app := fiber.New() // Create a simple struct for our item type Item struct { ID string `json:"id"` Name string `json:"name"` Price int `json:"price"` } items := []Item{ {ID: "1", Name: "Item One", Price: 100}, {ID: "2", Name: "Item Two", Price: 200}, } // GET all items app.Get("/items", func(c *fiber.Ctx) error { return c.JSON(items) }) // POST a new item app.Post("/items", func(c *fiber.Ctx) error { var item Item if err := c.BodyParser(&item); err != nil { return c.Status(400).SendString(err.Error()) } items = append(items, item) return c.Status(201).JSON(item) }) // Start the server app.Listen(":3000") }

Go REST API Fiber web framework CRUD application development