SoFunction
Updated on 2025-03-04

Go Fiber quickly builds an HTTP server

Introduction

HTTP API service is one of the most commonly used features in Go. You can write a powerful HTTP service in a few simple lines of code. This is the charm of Go.

It is very easy to implement an http server with Go. The Go language standard library net/http comes with a series of structures and methods to help developers simplify the relevant processes of HTTP service development. Therefore, we do not need to rely on any third-party components to build and start a highly concurrent HTTP server.

Fiber is an Express-inspired web framework based on fastthttp, the fastest Go http engine. Designed to be a simple and fast way to develop, while taking into account zero memory allocation and performance. Here, by default, you have already built a local Go environment.

1. Installation

go install /gofiber/fiber/v2@latest

2. Create a local project

After creating the local project, use go mod init to initialize the current folder as a Go Module and specify its import path.

go mod init Project name

3. Write Go code

Create a go file in the project directory

  • Importing dependencies for Fiber framework /gofiber/fiber/v2
  • Initialize a Fiber App with ()
  • Use ("/", "") to set the static file route, where the file directory is empty, return 404
  • Use () to set the GET route of /path and return "Hello World!"
  • Use (":3000") to start the server and listen to port 3000
  • Run the program and visit http://localhost:3000 to see the response of "Hello World!"

You can set the path of some HTML pages. After setting, you can access all static resources under this path by opening http://localhost:3000.

package main
import "/gofiber/fiber/v2"
func main() {
	app := ()
	("/", "")
	("/", func(c *) error {
		return ("Hello World!")
	})
	(":3000")
}

4. Open http://127.0.0.1:3000

This is the end of this article about Go Fiber quickly building an HTTP server. For more information about Go Fiber building an HTTP server, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!