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")
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?