Set up Go environment correctly on Mac

Rain Wu
Random Life Journal
3 min readMay 9, 2020

Continue from previous article Set up Python environment correctly on Mac, I will deal with Go environment for developing in this article. The abstract archtecture is very similar to Python, one layer for Go version management, and another for workspace isolation.

Fortunately, MAC does not pre-install Go, so we can save the effort for removing dependent tools. That’s fine, we can install the version manager directly !

gvm

gvm is a open source Go version management tool, the most powerful feature is helping you update related environment variables uniformly. When I am new to Go development one and a half years ago, I often suffer from env issues and spend plenty of time on searching for solutions.

$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

This tool needs to be initialized before use, use the source command to run the script or append into ~/.zshrc for automatic execution.

$ source /Users/rainwu/.gvm/scripts/gvm
$ echo -e ‘source /Users/rainwu/.gvm/scripts/gvm’ >> ~/.zshrc

Now we can install the specified version of Go, but we need to install the 1.4 version first. Becauce all version greater than 1.4 is compile via Go itself, I’m not sure if there’s another tips to deal with it, but we need a base Go version or it may cause some compile error.

In my case, I will use the Go 1.14, and I think the version greater than 1.11 would be a better choice because of the go mod feature which I will demonstrate later.

$ gvm install go1.4 -B
$ gvm install go1.14 -B

Set the version you prefer and make it permanently via — default flag if you want, just like what you do with pyenv in previous article.

$ gvm use go1.14 --default
$ gvm list
gvm gos (installed)=> go1.14
go1.4

go mod

go mod is a workspace management tools that makeover the Go project management. Before Go 1.11 version release, the project must be a child directory of the GOPATH, go mod lifts this restriction and greatly increases the flexibility of project management.

First initialize the workspace via init subcommand and the module name you like, it is highly recommended to make the module name consistent with your repository hierarchical management.

$ cd Repositories/HelloGolang
$ go mod init Repositories/HelloGolang

Install any package you often use to make sure it works correctly, and take a look at the go.mod file.

$ go get -u github.com/gin-gonic/gin
$ cat go.mod
module Repositories/HelloGolang
go 1.14require (
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/golang/protobuf v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
)

And…let’s give it a test!

$ go run main.go
Hello Golang!

The Go environment was deployed sucessfully now, please give me some claps if this article is helpful to you :)

--

--

Rain Wu
Random Life Journal

A software engineer specializing in distributed systems and cloud services, desire to realize various imaginations of future life through technology.