paint-brush
How One ExpressJS Programmer Visited Go Landby@israel-laguan
904 reads
904 reads

How One ExpressJS Programmer Visited Go Land

tldt arrow

Too Long; Didn't Read

Fiber is inspired by Express, the most popular web framework on the Internet. It combined the ease of Express and raw performance of Go. It's a good start for learn Go when you have experience in ExpressJS, what similarities and differences to expect, and what things I learned when building my first API on Go. How One ExpressJS Programmer Visited Go Land in Land of Land, Israel Antonio Rosales Laguan explains why Fiber is a good starting point for learning Go, and how to use it in a familiar environment.

Company Mentioned

Mention Thumbnail
featured image - How One ExpressJS Programmer Visited Go Land
Israel Antonio Rosales Laguan HackerNoon profile picture

I describe why Fiber it's really a good start for learn Go when you have experience in ExpressJS, what similarities and differences to expect, and what things I learned when building my first API on Go.

I learned to code in my Mathematics Career using Python and then C/C++. Since then I enjoyed to create code that solves real world problems. But 4 years ago I decided to freelance and found out that most of the people offers freelancer project doing Web Sites or Web Apps, and as I knew React (with class back then), I focused in web development.

But lately I found myself in the JS part of the now infamous Big Divide, enjoying to pass props, create Stores or build API code. So I preferred to work on backend, and ExpressJS was my preferred choice. I tried Sails, Nest or Hapi, and while they are great, I loved to code in my terms so I always came back to Express. I practiced a lot of it, learned my set of tricks and can easily make an API from scratch.

But as comfortable as I am on express, I have to grow. I have some technologies on my sight: Rust, Julia, Kotlin, Go. And I usually read some articles and documentation about them. But one day I received a call to action from GitHub about the last one, Go, and that proved to be a eureka moment for me! Here is the "Explore repositories" in my GitHub:

I was curious about

gofiber
, and why GitHub algorithm decided to present me that project. So I opened the repository and the idea of the project caught my attention:

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.

Are they talking to me? Can I use my experience in Express to learn and make a production ready API in a new language? Sounds kinda pretentious, but I decided to give it a try, why not?

First steps using Fiber as an ExpressJS user

Fiber have 3 sources of documentation from the start: the webpage, a recipes repository, and the README itself. The website in fact is really fancy and informative, the first section portrait how easy is to start using fiber:

package main

import "github.com/gofiber/fiber"

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

  app.Get("/", func (c *fiber.Ctx) {
    c.Send("Hello, World!")
  })

  app.Listen(3000)
}

Well, the first thing I noticed was that the methods

Listen
and
Send
was really similar to Express counterparts. Also the
Get
method have the same structure, the
path
and then a
callback
to resolve the route.

Well, the

Ctx
and the pointer (
*
) where the most notable differences for me. The pointer (and how to call/import a package or how to define a function) it's understandable as oddities of Go.
Ctx
method reminds me to a similar concept in Apollo Server, so I let it be.

Ctx
have most of the methods you are used to in expressJS: Send, Status, JSON, Next, Params, etc. And that the names are similar to their express counterparts is really welcome! I understood the logic of context almost intermediately, leaving me to learn mostly of all Go syntax. Here is the documentation of Context.

Another good point about Fiber is that most of the middlewares modules of express like body-parser, helmet, compression, etc., are available also in Fiber, maintained by the team or from an external developer, well documented and with examples.

That let any developer to be productive fast as you have all the tools from the start in a familiar environment.

Perks of learning Go for a ExpressJS user

Before trying Fiber I has already read most of A Tour of Go, but never pass from code snippets to a full fledged project. And that its sometimes a problem with learning a new language, you start to know some syntax and getting costumed to solve simple scoped problems, but learning how to organize your code in a bigger project or what to choose among several packages poses a problem later.

Fiber solved most of that problems for me when creating my first project in Go. I already knew most of the concepts of an API (like routes, middlewares, send Status and a response, etc.), and emulates what I already know in Go.

I even used my same Folder Structure!

While I didn't tried all the middlewares in my project, I can say that I am happy with my Go API built with Fiber! I fervently recommends Fiber to any Express Developer who want to start learning Go! Also, the benchmarks for Fiber are amazing, as you can check in this article:

What I learned of Go while using Fiber

As I told before, using Fiber let me concentrate in learn Go, and how to apply it in making an API. I will explain some of my findings.

The most important feature of Go comparing to JavaScript is that is compiled, not interpreted. In ExpressJS NodeJS is a middle layer between your code and the machine, because a Linux or Windows server don't "talk" in JavaScript, but that is different in Go, as it compiles into bitcode. When you finish your code in Go you have to options, run the code (like a preview) or you build the code to optimize it and let it ready for the machine to execute it.

$ go run main.go
Hello World from Go Land!
$ go build main.go
$ ./main
Hello World from Go Land!

That reminds me when you make code using webpack, you can see a develop version (ussualy with Hot Reload) and a production version that you have to build. So in Go is like that but better, your code is optimized until is a lot better than their JS counterpart, that's why more and more companies are starting to migrate to Go (you can check some of them here).

Another point of distress to me is how I miss (or got too attached) to Object Destructuring or Arrow Functions. A big point of confusion to me at first was about how to export functions/variables. Importing is kinda transparent:

import "some/package"

import(
  "./local/package"
  "github.com/author/repo"

  name "local/package"
)

But exporting was hard to get at first, as in JS you can name explicitly what you want to export (while name conventions are not enforced), but in Go if the first letter is Uppercase its exported, if not is private, so you have to take extra care while naming in go.

// Uppercase named function can be imported from another package
func Uppercase() {
}

// This function it's private
func lowercase() {
}

Also note that Go enforces you to add comments to any exported function, what to me seems correct as that comments are important if you import third-party/not yours packages.

Overall my experience with Fiber is great, I am still learning more Go concepts and honing my skills on pointers and concurrency. Any typo, correction or comment about the article?

Please contact me!, I would be happy to talk with you!