loading...

Getting started with Fiber

fenny profile image Fenny ・3 min read

New gophers that make the switch from Node.js to Go are dealing with a learning curve before they can start building their web applications or microservices. Fiber, as a web framework, was created with the idea of minimalism and follows the UNIX way, so that new gophers can quickly enter the world of Go with a warm and trusted welcome.

Fiber is inspired by Express, the most popular web framework on the Internet. We combined the ease of Express and raw performance of Go. If you have ever implemented a web application in Node.js (using Express or similar), then many methods and principles will seem very common to you.

We listen to our users in issues, Discord channel and all over the Internet to create a fast, flexible and friendly Go web framework for any task, deadline and developer skillset! Just like Express does in the JavaScript world.

Step 1: Install Go

You can get the latest version of Go from the official Golang website follow the instructions and you're good to go, make sure you install Go 1.14 or higher.

To confirm you have Go installed open your terminal or command prompt and run the following command:

> go version
go version go1.15 windows/amd64

You should get the version number of your Go installation.

Step 2: Install Fiber

After Successfully installing Go you can proceed with installing Fiber. Installation is done using the go get command:

> go get github.com/gofiber/fiber/v2
go: downloading github.com/gofiber/fiber/v2 v2.0.5
go: github.com/gofiber/fiber/v2 upgrade => v2.0.5

This will download Fiber v2 which we will use to create our first Hello, World πŸ‘‹! app.

Step 3: Creating Web Server

In your project directory, we want to create a go.mod to manage our dependencies. Type the following:

> go mod init demo
go: creating new go.mod: module demo

Now create a file called main.go in the project directory write the following code in it:

package main

import (
    "log"

    "github.com/gofiber/fiber/v2"
)

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

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello, World πŸ‘‹!")
    })

    log.Fatal(app.Listen(":3000"))
}

The above code is actually the bare-bones to get the server respond with a Hello, World πŸ‘‹!

Save the file and run the following command to update your go.mod

> go mod tidy
go: finding module for package github.com/gofiber/fiber/v2
go: found github.com/gofiber/fiber/v2 in github.com/gofiber/fiber/v2 v2.0.5

This will create a go.sum file that ensures that future downloads of these modules retrieve the same bits as the first download, to ensure the modules your project depends on do not change unexpectedly, whether for malicious, accidental, or other reasons

Your project folder should contain the following file structure:

/GettingStarted
   β”œβ”€β”€ main.go
   β”œβ”€β”€ go.mod
   └── go.sum

Let's launch our app by calling the following command:

go run main.go

Visit http://localhost:3000 in your browser. You will see the following:

At this point, you have successfully set up your first Fiber application.

Step 4: Explore Fiber

Fiber has a great community and online documentation that can help you creating fast and secure web apps.

See ya πŸ‘‹

Posted on by:

fenny profile

Fenny

@fenny

Senior Go and Node.js Developer

Discussion

pic
Editor guide
 

Greetings! I've been wanting to try Go for a while and this looks awesome coming from a Node background. I really want to see this take off. Do you have any examples of this working with SSO (SAML, in particular?) because we utilize Shibboleth where I work. I'd love to transition some of our APIs to use this instead of Express. Great work on this!

 

Awesome article! Thanks :)

 

really nice intro to fiber! You should made more fiber tutorials/guides especially since you created it l!

 

Great introduction to fiber! So easy to start a web server with tons of features out of box.

 

You're doing really good work fenny, the fiber community is very welcoming