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.
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.
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)/binair -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.
Now in the project directory you should see.
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.