Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 5, 2020 03:29 am GMT

Getting started with Fiber

New gophers that make the switch from Node.js to Go are dealing with a learning curve before they can start building their web applications or microservices. Fiber, as a web framework, was created with the idea of minimalism and follows the UNIX way, so that new gophers can quickly enter the world of Go with a warm and trusted welcome.

Fiber is inspired by Express, the most popular web framework on the Internet. We combined the ease of Express and raw performance of Go. If you have ever implemented a web application in Node.js (using Express or similar), then many methods and principles will seem very common to you.

We listen to our users in issues, Discord channel and all over the Internet to create a fast, flexible and friendly Go web framework for any task, deadline and developer skillset! Just like Express does in the JavaScript world.

Step 1: Install Go

You can get the latest version of Go from the official Golang website follow the instructions and you're good to go, make sure you install Go 1.14 or higher.

To confirm you have Go installed open your terminal or command prompt and run the following command:

> go versiongo version go1.15 windows/amd64

You should get the version number of your Go installation.

Step 2: Install Fiber

After Successfully installing Go you can proceed with installing Fiber. Installation is done using the go get command:

> go get github.com/gofiber/fiber/v2go: downloading github.com/gofiber/fiber/v2 v2.0.5go: github.com/gofiber/fiber/v2 upgrade => v2.0.5

This will download Fiber v2 which we will use to create our first Hello, World ! app.

Step 3: Creating Web Server

In your project directory, we want to create a go.mod to manage our dependencies. Type the following:

> go mod init demogo: creating new go.mod: module demo

Now create a file called main.go in the project directory write the following code in it:

package mainimport (    "log"    "github.com/gofiber/fiber/v2")func main() {    app := fiber.New()    app.Get("/", func(c *fiber.Ctx) error {        return c.SendString("Hello, World !")    })    log.Fatal(app.Listen(":3000"))}

The above code is actually the bare-bones to get the server respond with a Hello, World !

Save the file and run the following command to update your go.mod

> go mod tidygo: finding module for package github.com/gofiber/fiber/v2go: found github.com/gofiber/fiber/v2 in github.com/gofiber/fiber/v2 v2.0.5

This will create a go.sum file that ensures that future downloads of these modules retrieve the same bits as the first download, to ensure the modules your project depends on do not change unexpectedly, whether for malicious, accidental, or other reasons

Your project folder should contain the following file structure:

/GettingStarted    main.go    go.mod    go.sum

Let's launch our app by calling the following command:

go run main.go

Visit http://localhost:3000 in your browser. You will see the following:

At this point, you have successfully set up your first Fiber application.

Step 4: Explore Fiber

Fiber has a great community and online documentation that can help you creating fast and secure web apps.

See ya


Original Link: https://dev.to/fenny/getting-started-with-fiber-36b6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To