Fiber for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Fiber and why do you need it?

\n\n

Fiber is a fast web framework for Go, inspired by the popular Express.js framework for Node.js. It is designed for building high-performance web applications and APIs with minimal memory consumption. Fiber uses a non-blocking I/O model based on goroutines and the fasthttp package, making it one of the fastest web frameworks for Go.

\n\n

Why do you need Fiber?

\n
    \n
  • Performance: Fiber works significantly faster than the standard net/http package by using fasthttp. It can handle tens of thousands of requests per second.
  • \n
  • Simplicity and familiar syntax: If you are familiar with Express.js, you will quickly learn Fiber. The API is very similar: routes, middleware, request handlers — everything is intuitive.
  • \n
  • Zero memory allocation: Fiber strives to minimize memory allocation, which reduces load on the garbage collector and improves stability.
  • \n
  • Extensibility: A large number of built-in middleware (CORS, logging, compression, sessions, etc.) and the ability to create your own.
  • \n
  • Support for modern standards: Fiber supports HTTP/2, WebSocket, template engines, static files, and much more.
  • \n
\n\n

Installing Fiber

\n\n

Fiber is a library for Go, so installation is done through the standard Go package manager. Make sure you have Go version 1.16 or higher installed.

\n\n
# Initialize module (if not already done)\ngo mod init myapp\n\n# Install Fiber (latest stable version)\ngo get github.com/gofiber/fiber/v2
\n\n

After that, you can import Fiber into your project:

\n\n
import "github.com/gofiber/fiber/v2"
\n\n

Note: If you are using an older version of Go, you may need to install Fiber v1. However, it is recommended to use v2 as it is more actively developed.

\n\n

Quick start with code example

\n\n

Create a file main.go and write a simple HTTP server:

\n\n
package main\n\nimport "github.com/gofiber/fiber/v2"\n\nfunc main() {\n    // Create a new Fiber application\n    app := fiber.New()\n\n    // Define GET route /\n    app.Get("/", func(c *fiber.Ctx) error {\n        return c.SendString("Hello, Fiber!")\n    })\n\n    // Start server on port 3000\n    app.Listen(":3000")\n}
\n\n

Run the server:

\n\n
go run main.go
\n\n

Open your browser and go to http://localhost:3000. You will see the message "Hello, Fiber!".

\n\n

Explanation:

\n
    \n
  • fiber.New() — creates a new application instance.
  • \n
  • app.Get("/", handler) — registers a handler for a GET request to the root path.
  • \n
  • c *fiber.Ctx — request context, providing access to request and response data.
  • \n
  • c.SendString() — sends a string as a response.
  • \n
  • app.Listen(":3000") — starts the server on port 3000.
  • \n
\n\n

Main methods and functions of Fiber

\n\n

Let's look at 10 key methods and functions of Fiber with detailed descriptions and examples.

\n\n

1. fiber.New()

\n

What it does: Creates a new Fiber application instance with default settings or with passed configuration.

\n

Parameters: Accepts an optional argument config ...fiber.Config — a structure with settings (e.g., AppName, Prefork, ServerHeader, etc.).

\n

Example:

\n
package main\n\nimport "github.com/gofiber/fiber/v2"\n\nfunc main() {\                    

Recommendations