description
An online API documentation with examples so you can start building web apps with Fiber right away!

📖 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.

Installation

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

Installation is done using the go get command:

  1. go get -u github.com/gofiber/fiber

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) {
  2. result := c.Param("foo") // result is only valid within this method
  3. }

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) {
  2. result := c.Param("foo") // result is only valid within this method
  3. newBuffer := make([]byte, len(result))
  4. copy(newBuffer, result)
  5. newResult := string(newBuffer) // newResult is immutable and valid forever
  6. }

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

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

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"
  3. func main() {
  4. app := fiber.New()
  5. app.Get("/", func(c *fiber.Ctx) {
  6. c.Send("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))
  • 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

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

Parameters

  1. // GET http://localhost:8080/hello%20world
  2. app.Get("/:value", func(c *fiber.Ctx) {
  3. c.Send("Get request with 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) {
  3. if c.Params("name") != "" {
  4. c.Send("Hello " + c.Params("name"))
  5. // => Hello john
  6. } else {
  7. c.Send("Where is john?")
  8. }
  9. })

Wildcards

  1. // GET http://localhost:3000/api/user/john
  2. app.Get("/api/*", func(c *fiber.Ctx) {
  3. c.Send("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(8080)

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