👋 Welcome

Fiber is an Express inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development with zero memory allocation and performance in mind.

These docs are for Fiber v2, which was released on September 15th, 2020.

Installation

First of all, download and install Go. 1.14 or higher is required.

Installation is done using the go get command:

  1. go get github.com/gofiber/fiber/v2

Zero Allocation

Some values returned from *fiber.Ctx are not immutable by default

Because fiber is optimized for high-performance, values returned from fiber.Ctx are not immutable by default and will be re-used across requests. As a rule of thumb, you must only use context values within the handler, and you must not keep any references. As soon as you return from the handler, any values you have obtained from the context will be re-used in future requests and will change below your feet. Here is an example:

  1. func handler(c *fiber.Ctx) error {
  2. // Variable is only valid within this handler
  3. result := c.Params("foo")
  4. // ...
  5. }

If you need to persist such values outside the handler, make copies of their underlying buffer using the copy builtin. Here is an example for persisting a string:

  1. func handler(c *fiber.Ctx) error {
  2. // Variable is only valid within this handler
  3. result := c.Params("foo")
  4. // Make a copy
  5. buffer := make([]byte, len(result))
  6. copy(buffer, result)
  7. resultCopy := string(buffer)
  8. // Variable is now valid forever
  9. // ...
  10. }

We created a custom ImmutableString a function that does the above and is available in the gofiber/utils package.

  1. app.Get("/:foo", func(c *fiber.Ctx) error {
  2. // Variable is now immutable
  3. result := utils.ImmutableString(c.Params("foo"))
  4. // ...
  5. })

Alternatively, you can also use the Immutable setting. It will make all values returned from the context immutable, allowing you to persist them anywhere. Of course, this comes at the cost of performance.

For more information, please check #426 and #185.

Hello, World!

Embedded below is essentially the most straightforward Fiber app, which you can create.

  1. package main
  2. import "github.com/gofiber/fiber/v2"
  3. func main() {
  4. app := fiber.New()
  5. app.Get("/", func(c *fiber.Ctx) error {
  6. return c.SendString("Hello, World!")
  7. })
  8. app.Listen(":3000")
  9. }
  1. go run server.go

Browse to http://localhost:3000, and you should see Hello, World! on the page.

Basic routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, PUT, POST and so on).

Each route can have multiple handler functions, that is executed when the route is matched.

Route definition takes the following structures:

  1. // Function signature
  2. app.Method(path string, ...func(*fiber.Ctx) error)
  • app is an instance of Fiber.
  • Method is an HTTP request method, in capitalization: Get, Put, Post, etc.
  • path is a virtual path on the server.
  • func(*fiber.Ctx) error is a callback function containing the Context executed when the route is matched.

Simple route

  1. // Respond with "Hello, World!" on root path, "/"
  2. app.Get("/", func(c *fiber.Ctx) error {
  3. return c.SendString("Hello, World!")
  4. })

Parameters

  1. // GET http://localhost:8080/hello%20world
  2. app.Get("/:value", func(c *fiber.Ctx) error {
  3. return c.SendString("value: " + c.Params("value"))
  4. // => Get request with value: hello world
  5. })

Optional parameter

  1. // GET http://localhost:3000/john
  2. app.Get("/:name?", func(c *fiber.Ctx) error {
  3. if c.Params("name") != "" {
  4. return c.SendString("Hello " + c.Params("name"))
  5. // => Hello john
  6. }
  7. return c.SendString("Where is john?")
  8. })

Wildcards

  1. // GET http://localhost:3000/api/user/john
  2. app.Get("/api/*", func(c *fiber.Ctx) error {
  3. return c.SendString("API path: " + c.Params("*"))
  4. // => API path: user/john
  5. })

Static files

To serve static files such as images, CSS, and JavaScript files, replace your function handler with a file or directory string.

Function signature:

  1. app.Static(prefix, root string)

Use the following code to serve files in a directory named ./public:

  1. app := fiber.New()
  2. app.Static("/", "./public")
  3. app.Listen(":3000")

Now, you can load the files that are in the ./public directory:

  1. http://localhost:8080/hello.html
  2. http://localhost:8080/js/jquery.js
  3. http://localhost:8080/css/style.css

Note

For more information on how to build APIs in Go with Fiber, please check out this excellent article on building an express-style API in Go with Fiber