Photo by Florian Olivo on Unsplash

Backend Development with Go Part 3

Jonathan Reeves
5 min readOct 19, 2021

--

Hello. I know that it has been a little bit since I have said anything on this. I have been dealing with some personal things and haven’t had much time to write this up. I am now however in a decent spot to where I can give this update.

As you know this series is supposed to be following my journey through building an API using the Go programming language. I am very much in love with Go at the moment. I just really like everything about the language. I like how it handles package management and how there is a vast amount of support from the standard library. But enough about Go and it’s standard library or my love of the language. Let’s get into the nitty gritty details.

Using Fiber the Express.js Equal for Go

I am currently building a project using the Fiber package.

Fiber is a Go web framework built on top of Fasthttp, the fastest HTTP engine for Go. It’s designed to ease things up for fast development with zero memory allocation and performance in mind.

Fiber is based off of Express.js. If you have used Express before or are currently using it but looking at learning Go for web development then I would have to say look no further. Fiber is a really easy to use library and much of the concepts of developing APIs and web servers is similar in Fiber. For those interested you can check out the official site for fiber.

I also started using Gin which is another library for writing web servers and APIs. This is also a great library for web development. I have been impressed with this one as well. The official site for Gin. If you are looking for something that is more Go-like in nature I would suggest Gin. If you have never used Express or if you don’t want to use something that is modeled after a JavaScript library then I would also recommend Gin. To compare the two frameworks we have code snippets below. We will start with Fiber:

Fiber — Hello World

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

Now with Fiber this is pretty straight forward. If you have used Express.js before you will notice similarities. It is a really great framework for web development. Now let’s look at Gin.

Gin — Hello World(Ping, Pong)

package main

import "github.com/gin-gonic/gin"

func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}

As you can see Gin is a bit more Go-like. The syntax is very much like writing Go code. The functionality is still straightforward and easy to see what is happening. If you use Go as your main language and haven’t used Node then Gin is the right call for you.

If you are new to Go and you are coming from a JavaScript background such as myself I highly recommend using Fiber. On the other hand if you are new to programming and have only worked with Go I highly recommend Gin as it will look more closely to what you know.

Basic Project Structure: Fiber

├── Dockerfile
├── docker-compose.yaml
├── go.mod
├── go.sum
├── main.go
├── src
│ ├── controllers
│ │ └── authController.go
│ ├── database
│ │ └── db.go
│ ├── models
│ │ └── user.go
│ └── routes
│ └── routes.go
└── tmp
├── build-errors.log
└── main

Above we have the project structure for the project with Fiber. Again if you come from Node and Express you will notice that it is pretty similar. Just like with Node and Express you will have your project like:

├── src
│ ├── controllers
│ ├── transactionController.js
│ ├── models
│ │ └── Transaction.js
│ └── routes
│ └── transactions.js
└──package-lock.json
├── package.json
└── server.js

As you can see between the two projects the structure is similar. So if you come from Express the structure and the way you go about building the routes will be similar. Which means you can pick up on things fairly quickly.

This is the approach I am taking for this particular project and will continue with Gin for another project. I will go over the Gin project at a later time.

Working with Docker

As you can see from the printout of my directory, I am using Docker to containerize my project. I am also using a MySQL instance from within a Docker Container. For those of you new to Docker, Docker helps you distribute your application easier by putting the app inside a container which is basically a mini virtual machine that only has enough resources needed to run your app. It’s basically like having a small Virtualbox that only runs your application. This Virtual Machine or container can then be pushed up to Docker Hub, which is similar to GitHub, you can push your entire app image up and have a friend or co worker pull your image and run it without the need to install all the packages and libraries the project needs in order to run. Makes life really simple at the end of the day.

Why MySQL

Some of you may be wondering why MySQL, well the truth is that I have experience adding MySQL to Docker already so I decided to go with what I already know vs trying something different. With the Gin project I am actually using MongoDB so that will be something in that project that I talk about. In theory though you can use any database you are familiar with.

Conclusion

I hope this gives a pretty good understanding of the framework I decided to go with. I also hope that it helps anyone looking into using Go for web development, whether that be by someone from the JavaScript world or C/C++ world that has been using Go. I believe that both frameworks are great in their own ways. I can’t say that one is better than the other as I feel just like with all frameworks it depends on the use case and the comfortability of the user when choosing which one to ultimately use. I can say that if you come from a JavaScript background using Express try out Fiber first, then try out Gin and make a decision that way. If you are already using Go and want to try a new framework other than using the built in net/http package for handling API development then try Gin and then try Fiber to see which one you like using better.

--

--

Jonathan Reeves

I am a software engineer that is currently trying to break into the DevOps world using Python. Professionally I use JavaScript with React to build websites.