DEV Community

Cover image for Setting Up a Simple Go Fiber Project
luthfisauqi17
luthfisauqi17

Posted on

Setting Up a Simple Go Fiber Project


In this tutorial, we will go step by step to set up a basic web server using Go Fiber. By the end of this guide, you will have a running Go Fiber application that responds with "Hello, World!" when accessed.

Step 1: Install Go

Before we start, ensure that Go is installed on your system. You can verify this by running:

go version
Enter fullscreen mode Exit fullscreen mode

If you don’t have Go installed, download and install it from golang.org.

Step 2: Create a New Go Project

Navigate to your desired directory and create a new project folder:

mkdir go-fiber-app && cd go-fiber-app
Enter fullscreen mode Exit fullscreen mode

Initialize a Go module:

go mod init go-fiber-app
Enter fullscreen mode Exit fullscreen mode

This will create a go.mod file, which manages dependencies for your project.

Step 3: Install Fiber

Run the following command to install Fiber:

go get github.com/gofiber/fiber/v2
Enter fullscreen mode Exit fullscreen mode

This fetches the Fiber package and adds it to your go.mod file.

Step 4: Create a Simple Fiber Server

Create a new file named main.go and add the following code:

package main

import (
    "log"
    "github.com/gofiber/fiber/v2"
)

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

    // Define a simple route
    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello, World!")
    })

    // Start the server
    log.Fatal(app.Listen(":3000"))
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Run the Fiber App

Now, let's run our Go Fiber application:

go run main.go
Enter fullscreen mode Exit fullscreen mode

Open your browser and visit http://localhost:3000. You should see:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

There you go, that is how you setting up a simple golang Fiber project. Thank you for reading, and have a nice day!

Top comments (0)