Skip to main content
Version: v1.x

📖 Getting started

Fiber is an Express inspired web framework build 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.

caution

These docs are for the old Fiber v1, you can view the v2 docs here.
v2.0.0 was released on September 15th, 2020, and contains numerous changes regarding performance and API design.

Installation

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

Installation is done using the go get command:

go get -u github.com/gofiber/fiber

Zero Allocation

caution

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:

func handler(c *fiber.Ctx) {
result := c.Param("foo") // result is only valid within this method
}

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:

func handler(c *fiber.Ctx) {
result := c.Param("foo") // result is only valid within this method
newBuffer := make([]byte, len(result))
copy(newBuffer, result)
newResult := string(newBuffer) // newResult is immutable and valid forever
}

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

app.Get("/:foo", func(c *fiber.Ctx) {
result := utils.ImmutableString(c.Param("foo"))
// result is now immutable
})

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.

package main

import "github.com/gofiber/fiber"

func main() {
app := fiber.New()

app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})

app.Listen(3000)
}
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).

info

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

Route definition takes the following structures:

// Function signature
app.Method(path string, ...func(*fiber.Ctx))
  • 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) is a callback function containing the Context executed when the route is matched.

Simple route

// Respond with "Hello, World!" on root path, "/"
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})

Parameters

// GET http://localhost:8080/hello%20world

app.Get("/:value", func(c *fiber.Ctx) {
c.Send("Get request with value: " + c.Params("value"))
// => Get request with value: hello world
})

Optional parameter

// GET http://localhost:3000/john

app.Get("/:name?", func(c *fiber.Ctx) {
if c.Params("name") != "" {
c.Send("Hello " + c.Params("name"))
// => Hello john
} else {
c.Send("Where is john?")
}
})

Wildcards

// GET http://localhost:3000/api/user/john

app.Get("/api/*", func(c *fiber.Ctx) {
c.Send("API path: " + c.Params("*"))
// => API path: user/john
})

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:

app.Static(prefix, root string)

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

app := fiber.New()

app.Static("/", "./public")

app.Listen(8080)

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

http://localhost:8080/hello.html
http://localhost:8080/js/jquery.js
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