NodeJS to Golang: Live-reloading MacOS

Kafka
2 min readJun 1, 2021
Here's what I'm trying to get set up in this quick tutorial.

Sharing my experience switching from NodeJS to Golang. I spent a bit of time trying to get my workflow setup the way I had it with NodeJS. Here are the steps I went through getting a proper workflow set up for Golang development.

Installing

I use HomeBrew for MacOS. HomeBrew is a package manager like npm for NodeJS. I highly recommend it and will be using it for this example.

brew install go

Start a new project

We are going to create a hello world example to show how to get projects started.

mkdir hello
cd hello
go mod init example.com/hello

Here we create new directory “hello” then we cd into the directory. Finially, the go mod init command creates a go.mod file to track your code's dependencies. ( Just like package.json in a NodeJS application )

Live-reloading — remember nodemon

I recommend air for Golang its the best I have found so far.

# binary will be $(go env GOPATH)/bin/air
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
air -v

This will install in your Go folder
for me it was “/Users/kafka/go/bin/air

Error: “command not found: air”
So for me to fix this I opened up .zshrc:

code ~/.zshrc # add this line to bottom of this file
export PATH=$PATH:$(go env GOPATH)/bin

My .zshrc location is: “/Users/kafka/.zshrc”

Once you have air cmd working. We head back to our hello directory.
We need to initialize air config with the following cmd.

air init

Now in the project directory you should see.

hello.go
.air.toml
go.mod

Example:

Now each time you save with “cmd + s “ air will recompile your program and run. Now we have a proper workflow.

I kept it short and sweet.

--

--

Kafka

“Genius” is 1% inspiration and 99% perspiration. Accordingly, a ‘genius’ is often merely a talented person who has done all of his homework — T.E.