Go: Workspaces

Mac Edition

Chris Kakos
The Startup
5 min readSep 28, 2020

--

courtesy of ashleymcnamara

Environment variables store a PATH that points to a specific location on your local machine. In order to process a program in Go, there are two environment variables needed to house the compilation of Go source code. These variables are referred to as GOPATH and GOROOT. Each one stores the location of a PATH needed for the compiler to execute Go source code in a conventional manner.

Navigating these environment variables takes some configuration. You must define the PATH setting in order for them to work properly. GOPATH must point to your Go workspace and GOROOT points to the installation directory.

Various terminals use different delimiters to access these environment variables. Unix-based OS’s — like Mac and Linux — use a dollar sign preceding the identifier — $GOPATH. Windows Operating Systems use a percent sign on both ends of the identifier — %GOPATH%.

DISCLAIMER: It is important to note that with the introduction of Go modules in v1.1, the following convention is more flexible. You can now run your Go programs and applications outside of your $GOPATH environment and your code will execute just the same. Nonetheless, I still use this convention as a means of structure and suggest you organize your code as such.

DOWNLOAD

You can download and install Go two different ways. One way is via terminal with brew by running the command:

$ brew install go

To download and install via macOS package installer, visit golang.org and click the `Download Go` button.

After it’s finished downloading, navigate to the installer package in your Downloads folder — or wherever you chose to download the files.

Keep everything default for now. You can always modify anything you’d like post installation — select continue all the way through.

Once the installation has complete, open a terminal a run the following command:

$ go version

If the version number prints out, then you’ve download and installed Go to your local machine.

WORKSPACE CONFIGURATION

After installing Go, you have to configure your workspace. A Go workspace is a directory stored on your local machine that houses code specific to Go. Any code written in Go should be saved to your workspace in order for the compiler to operate at optimal efficiency.

You can name this directory anything you’d like and store it at a location of your choosing. The path to your workspace will later be stored in an environment variable named $GOPATH.

However — inside of that workspace — you must create 3 subdirectories named by convention:

$GOPATH/src

src is where all of your Go projects and programs are located. It handles namespacing package management for all your Go repos.

$GOPATH/pkg

pkg stores archived files of packages installed in programs. This essentially helps to save compilation time based on whether the packages being used have been modified.

$GOPATH/bin

bin stores all of your compiled binaries.

C:/Users/chriskakos
C:/Users/chriskakos/go
C:/Users/chriskakos/go/workspace

ENVIRONMENT VARIABLES

Once you’ve created and named your workspace and subdirectories, you need to instruct the Go tools as to where this workspace is located. This is going to point your workspace to the $GOPATH environment variable. You can do this one of two ways.

NOTE: I use the .bashrc file to configure my local environment. If you are using another terminal such as ZSH, you would be configuring the .zshrc file.

$GOPATH

  1. If you’d like to configure your workspace from the command line — make sure to be in the root directory — and run the following command all on the same line:

$ echo "export GOPATH=/Users/chriskakos/go/workspace" >> .bashrc

Check to make sure the command ran properly:

$ cat .bashrc

Restart your terminal and verify that the Go tools now points to your workspace:

$ echo $GOPATH

Your Go workspace PATH should print out.

2. You can also configure your $GOPATH environment variable by explicitly modifying the .bashrc file:

$ vi .bashrc

Again, restart your terminal and verify that the Go tools now points to your workspace:

$ echo $GOPATH

$GOROOT

The $GOROOT should be configured upon installation but let’s check to make sure:

$ echo $GOROOT

You should see path similar to mine: C:/Users/chriskakos/go

If it’s not pointing there, follow the same approach that you did for setting your $GOPATH environment variable, only this time set $GOROOT.

$ echo "export GOROOT=/Users/chriskakos/go" >> .bashrc

OR

$ vi .bashrc

I know its redundant but restart your terminal and check to make sure that everything was configured properly by running one of the following commands:

$ echo $GOROOT or $ go env

CONCLUSION

Your environment variables should now be set and pointing to the appropriate locations. You’re ready to code. Again, with the newer versions of Go — version 1.1+ —you should be able to run Go code from outside of your workspace but structure is important, so having everything stored in one location makes compilation more efficient.

You have a long way to Go — but at least you’re pointed in the right direction.

--

--

Chris Kakos
The Startup

Software developer skilled in ecosystems surrounding JavaScript.