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
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
Initialize a Go module:
go mod init go-fiber-app
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
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"))
}
Step 5: Run the Fiber App
Now, let's run our Go Fiber application:
go run main.go
Open your browser and visit http://localhost:3000. You should see:
Hello, World!
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)