"Do one thing every day that scares you."

Simple web server with Go Fiber

Hi everyone and happy new year 🙂

Fiber is one of Go web frameworks that is simple, intuitive and easy to use. There are a lot of features, but it is not fully featured like PHP Laravel.

It is mainly for API but can be used for websites with HTML templates, not just for SPA. To install Fiber just run this command:

go get github.com/gofiber/fiber/v2

This is example of running web server with one http hander:

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!")
	})

	app.Listen(":3000")
}

Line by line: first we say the name of current package. After that it includes Fiber itself. The main function in main.go file is the point where our program will start. Making instance of Fiber is easy, just call fiber.New(). This method can receive some params, but will cover them in another article.

The simplest way to declare a http handler is to call app.Get, for GET requests. Every handler returns an error and receive as param the Fiber context, where we can use response and request.
The server itself is running with app.Listen method. This is the easiest way to run production ready web server in Go Fiber.

Another features of Fiber are:

  • Routes
  • Grouping Routes
  • Templates
  • Error handling
  • Validation
  • Hooks
  • Middlewares
  • And many methods to works with request and response and modify headers.

Previous

(Само) Няколко любими снимки

Next

10x Faster TypeScript

1 Comment

  1. When considering treatment for altitude sickness, opt for known solutions like [URL=https://goldenedit.com/drugs/stromectol/ – generic stromectol online[/URL – . Explore acquiring this medication online to ensure your journey remains uninterrupted.

    To obtain the lowest-priced options for nizagara , browse our site today.

    Harnessing the power of advanced healthcare has never been easier with the https://kristinbwright.com/item/propecia/ . Whether you’re seeking a convenient way to manage your health needs, this item stands out as the ultimate option.

    Your comment is awaiting moderation.

Leave a Reply

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

Powered by WordPress & Theme by Anders Norén