Golang Development Utilities You Should Know
How to easily make use of simple yet meaningful development tools in your Go projects
Install a specific version of Go— if you do not have an existing one
The documentation page for this step: https://go.dev/doc/install
Go to your specific operating system and follow the steps.
Installing a specific version of Go — if you do have an existing older version installed
The documentation page for this step is again: https://go.dev/doc/install
For this, you need to perform a few extra steps.
Go to your terminal and enter:
$ go version
go version go1.16.7 darwin/amd64
$ which go
/usr/local/go/bin/go
First you need to remove the existing installation:
sudo rm -rf usr/local/go
Now type:
$ which go
No go version found
$ go version
go not installed
Great, now follow the steps here to install a newer version: https://go.dev/doc/install
If you installed a newer go version in the middle of your project…
and you do not have any existing packages installed
The cleanest way to switch your project’s go.mod
file to the latest Go version is by following these couple of steps:
Remove the line:
go 1.16
or whatever versison you have from yourgo.mod
file.In the terminal, run:
go mod tidy
. This will bring your Go project to the latest version.
If you convert your existing project to another go version…
and you have existing packages installed
Follow these two steps:
Enter the following command:
go mod edit -go <new-go-version>
# for example
go mod edit -go 1.19.1
2. Then:
go mod tidy
Active compiling on save in a project
Air is a great utility Go package that helps rebuild and execute the project’s main.go
on save or virtually any files on save (as we want it) without us typing it out to run it every single time.
So there’s no need to do: go run main.go
every time we make a change in our project. Simple saving the file we were working on will reload it.
It’s a very handy when developing larger projects and saves quite a lot of development time too.
Here’s how to set it up:
Make sure you have your go paths set up: go open up your bash or zsh profile file and include the following lines there:
# if using zsh
open ~/.zshrc
# include these lines at the bottom of the file
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
export PATH=$PATH:$(go env GOPATH)/bin
2. Install air in your project directory:
go install github.com/cosmtrek/air@latest
3. Use air:
air init
This will create a new .air.toml
file in your project’s root directory. Then enter:
air
And it will start watching for changes in your project!
For example, if we were making a REST API with the Gin web framework, we’ll see something of the sort in our terminal:
The verbose output you see here can all be configured using the .air.toml
config file for air.
Looks very neat, doesn’t it?
How to use environment variables in a Go project
Using the Go package GoDotEnv, we can read and access key value pairs of secrets such as Database URL, username and password, server directory paths, SSH keys, etc from .env
files in our project.
The first step is to install this package:
go install github.com/joho/godotenv/cmd/godotenv@latest
Now, make a .env file in your project folder and save your secrets there.
For access in our project, in our main.go file, we can simply do:
import (
"log"
"os"
"github.com/joho/godotenv"
)
func init(){
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
Running it inside the init function makes sure it gets called before the main function.
Getting any secret will then be possible by using:
mySecretKey := os.Getenv("MY_SECRET_KEY")
Using Taskfile for auto executing commands
A good development practise is to keep a list of commands to execute defined in a Makefile, which we can easily reference later on and execute with simple make commands.
Taskfile is a slightly more contextually verbose and human readable alternative to Makefile. Install it on your local machine with the instructions mentioned here: https://taskfile.dev/installation/
Now, in the root of your project directory, just make a new Taskfile:
touch Taskfile.yml
Now we can simply define a list of commands as follows:
version: "3"
tasks:
build:
desc: Build the app
cmds:
- GOFLAGS=-mod=mod go build -o bin/go-rest-api internal/main.go
run:
desc: Run the app
cmds:
- GOFLAGS=-mod=mod go run internal/main.go
and so on.
Quite handy for executing a bunch of complicated terminal command with short and simple, easy to type and remember notations.
Bonus: Awesome list of Go Packages
If you’re searching for a library for a specific use case in your project, it is much better to look through this GitHub repository and find a package that suits your purposes.
There are tools ranging from simple debugging for your Go programs all the way to Natural language processing libraries! Here is the repo link: https://github.com/avelino/awesome-go
A few parting words
So there you a few of the utilities that are so very crucial to be aware of for writing Go software. I personally had to sift through a vast number of websites when I was learning them, so curating these tasks and tools here in one place is a great way to reduce that giant inconvenience!
I just saved ya’ll a few hours of frustration over conflicting StackOverflow answers. See you next time!