Converting Node project to Go

Percy Pham
Nextzy
Published in
3 min readApr 10, 2020

Things you need to know to create a Go app

First time learning Go as a Node programmer, I created a simple project using Go. I tried to adapt tools from Node world like package management, lint and stuff, but it’s not easy at first, and some of them did not appear obvious. Therefore, I decided to list all the equivalent stuff of Node from Go.

NPM — package management

The first thing we care about when creating a new project is package manager. There’s a problem when you don’t want to learn Go from the official documentation, that is you would encounter some old mechanism in outside tutorials. Some of them is: gopath, dep, glide.

I got the feeling of something not right, so I came back to official documentation and found out Go Mod. It’s right there in the article How to Write Go Code.

I got a habit of learning new things from tutorials, because when I read Node and Java documentations (BTW, I know about Java too), they are so confusing, tutorials help me to understand them easier.

Lesson learned: things from Google, always start with official documentation, they already document all the good stuff.

Use Go mod

Nodemon — live reload

There are many packages that I found: fresh, gin, modd. Personally, I use fresh, it’s simple because: I found it first.

At this one, I found that there’s a problem: I cannot easily separate devDependencies out of dependencies in Go. I solved it with the help from Docker, I will explain about it in a future article.

Use either fresh or gin or modd

DotENV — loads environment variables from a .env file

There’s a equivalent dotenv is godotenv.

Use godotenv

Parsing JSON

It’s not something hard to find, but there’s an article that can help you to deal with JSON in Go:

How to Parse JSON in Golang (With Examples)

MongoDB Driver

It’s not hard to find either, there’s an article that can help you to deal with CRUD in MongoDB:

MongoDB Go Driver Tutorial

Testing

Go have its own testing framework, you can read this article to know how to write tests in Go:

Golang basics — writing unit tests

And to run all tests in you repo, simple run:

go test ./...

Web Framework

There is one big framework Gin (this gin is different from live reload Gin above), as I see, it has a lot of features in it.

For http routers, there are two of them I think that are good: Mux and Httprouter

In my first simple app, I use Mux, because I just want to build a simple Rest server, so I don’t want to use Gin, and I found Mux before Httprouter.

Project Layout and Structure

When I read example Go code, I see most of the authors put their code in some sort of folders, I was curious why they named those folders like that: (pkg, cmd, …). My colleague hand me an github link, it explains them all, turns out, Go have a standard project layout (interesting fact: we should not name a folder src)

About the structure, there’s an interesting talk about it:

GopherCon 2018: Kat Zien — How Do You Structure Your Go Apps

Conclusion

Those are some of the things that I found while creating my simple app. I’m still working on it now, if you have any thing that you find similar with Node stuff in Go, then let me know by commenting in this post.

--

--