How to Create a Go Module for “Hello World” Using Fiber on Ubuntu

How to Write a ‘Hello World’ Program in GoLang

If you’re exploring Go (Golang) for building web applications, you’ve probably heard about Fiber one of the fastest web frameworks for Go, inspired by Express.js. In this tutorial, you’ll learn how to create Go module and build a simple “Hello World” web service using Fiber on an Ubuntu system.

What Is Fiber?

Fiber is a lightweight, fast, and expressive web framework built on top of Fasthttp, which makes it one of the most efficient frameworks in the Go ecosystem. It is ideal for developing REST APIs, microservices, and production-grade web applications.

Why Use Fiber for Go Web Services

  • High performance: Built on Fasthttp, the fastest Go HTTP engine.
  • Simple syntax: Easy for developers coming from Express.js or Node.js.
  • Production-ready: Middleware support for security, logging, and error handling.
  • Cross-platform: Works seamlessly on Ubuntu, Windows, and macOS.

Step 1: Install Go on Ubuntu

Before creating your Go module, ensure Go is installed on your Ubuntu system.

sudo apt update
sudo apt install golang-go -y

Verify your installation:

go version

You should see an output like:

go version go1.22.3 linux/amd64

Step 2: Create a New Project Directory

Create a folder for your Fiber project.

mkdir go-fiber-hello
cd go-fiber-hello

Step 3: Initialize a Go Module

Run the following command to create a Go module:

go mod init github.com/yourusername/go-fiber-hello

This command generates a go.mod file that manages dependencies for your project.

Step 4: Install Fiber

Use the go get command to add Fiber to your project.

go get github.com/gofiber/fiber/v2

This will automatically add Fiber to your Go module dependencies.

Step 5: Create the “Hello World” App

Create a new file named main.go inside your project directory:

nano main.go

Add the following Go code:

package main

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

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

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello, World! Welcome to Go Fiber Web Service 🚀")
    })

    app.Listen(":3000")
}

Step 6: Run the Fiber App

Now, run your application with:

go run main.go

If successful, you’ll see:

 ┌───────────────────────────────────┐ 
 │ Fiber v2.52.0 is running at :3000 │ 
 └───────────────────────────────────┘

Open your browser and visit:

http://localhost:3000

You’ll see the message:

Hello, World! Welcome to Go Fiber Web Service 🚀

Step 7: (Optional) Build the Binary

To compile the program into a standalone binary, run:

go build -o fiber-hello

Then, execute:

./fiber-hello

Your Fiber web service will start instantly.

Troubleshooting Common Issues

  • Error: go: command not found
    → Install Go using sudo apt install golang-go.
  • Port already in use
    → Change the app.Listen(":3000") to a different port like :8080.
  • Permission denied
    → Ensure you have proper file permissions: chmod +x fiber-hello.

Conclusion

You’ve successfully created your first Go module and “Hello World” web server using Fiber on Ubuntu. This is the perfect starting point for developing RESTful APIs, microservices, or production-grade Go web applications.

By following this step-by-step guide, you’re now ready to:

  • Extend your app with routing, middleware, and error handling.
  • Deploy your Go web service to production servers or Docker environments.

Quick Reference Summary

StepCommandDescription
1sudo apt install golang-goInstall Go
2mkdir go-fiber-helloCreate project folder
3go mod init <module-name>Initialize Go module
4go get github.com/gofiber/fiber/v2Install Fiber
5go run main.goRun app
6go build -o fiber-helloBuild binary

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *