In the spirit of DRY (Don’t Repeat Yourself), I’ve created this post to cover some of the other environment variables which may not be exposed in a typical Go installation. In future post’s i’ll refer back to these guidelines to help others with their environment settings.
An complete list of Go environment can be found by searching the following page or this heading: General-purpose environment variables.
Usually the installation will create a GOPATH environment variable for you. This location will be the development tree that you’re working in. You can learn more about here about how to configure GOPATH for your environment.
The Go community seems to be leaning more toward thinning out the dependency on environment variables you need to configure by providing good default behaviors when they’re not set. A good example of this is in providing a default bin or pkg location where your executables and packages are installed.
# Linux default locations $GOPATH/bin $GOPATH/pkg # Windows %GOPATH%\bin %GOPATH%\pkg
If you’re not yet seeing these locations you may not yet have built and installed an application or had any dependencies on packages.
These default locations are generally good, but there are times when you may need to override the default settings to help Go figure out your intent.
If you have multiple versions of Go installed you can identify the version you intend to use for a specific project by setting the GOROOT environment variable. In the prior link above to the Go document page, they describe this variable as: The root of the go tree. This is the folder location where Go is installed on your OS, which isn’t necessarily where your development tree is located. For me, when developing on Windows, I install Go in a tools folder like this.
# location of my Windows Go installation. C:\Tools\Go # env GOROOT setting C:> set GOROOT=C:\Tools\Go # location of my Go development tree C:> echo %GOPATH% C:\Home\Dev\Go # on my linux Raspberry Pi go was installed here /usr/lib/go-1.11/ # env setting $ export GOROOT=/usr/lib/go-1.11/ # go path $ echo $GOPATH /home/mitch/src/go
I like to keep code and programs out of places where helpful admins or automated scripts will try to back them up. I don’t typically back up my code, I check it in to Github. And, I don’t backup my executables, if needed again I install them.
Install the generate_cert utility
Let’s set GOBIN so we can install from GOROOT to our GOPATH/bin folder. In my post on enabling TLS for etcd we have a dependence on the generate_cert utility which is included as source code. We will build and install this utility in our GOPATH/bin directory by setting the GOBIN environment variable to point to that location. When we run go install it will use GOBIN to determine where to copy the executable.
# set location of GOBIN in Linux export GOBIN=$GOPATH/bin cd $GOROOT/src/crypto/tls/ go install generate_cert.go # Windows set GOBIN=%GOPATH%\bin cd %GOROOT%\src\crypto\tls go install generate_cert.go
To make any of these environment changes permanent you might add them to your Linux .bashrc file or Windows environment settings. More details can be found in my earlier link above to configure GOPATH. I will typically add GOPATH/bin to my path so I can run all the way cool utilities I create.
# add to linux path in .bachrc export PATH=$PATH:$GOPATH/bin # somewhere, in my windows environment settings, i add to PATH ...;%GOPATH%\bin;...
There’s another handy environment variable GOARCH, that I discussed in my post cross compiling for the Raspberry Pi. By setting this variable you can build a Go executable on Windows, Linux, etc in the proper format to run on another target operating system and hardware.
To remain consistent with DRY, I may update this section in future posts that depend on environment variables for their use cases.
I hope you found this section helpful!