Configuring Go
Go is now installed, but in order to use the tools, we must ensure that it is properly configured. To make calling the tools easier, we need to add our go/bin
path to the PATH
environment variable.
Note
On Unix systems, you should add export PATH=$PATH:/opt/go/bin
(make sure it is the path you chose when installing Go) to your .bashrc
file.
On Windows, open System Properties (try right-clicking on My Computer), and under Advanced, click on the Environment Variables button and use the UI to ensure that the PATH
variable contains the path to your go/bin
folder.
In a terminal (you may need to restart it for your changes to take effect), you can make sure this worked by printing the value of the PATH
variable:
echo $PATH
Ensure that the value printed contains the correct path to your go/bin
folder; for example, on my machine it prints as follows:
/usr/local/bin:/usr/bin:/bin:/opt/go/bin
Note
The colons (semicolons on Windows) between the paths indicate that the PATH
variable is actually a list of folders rather than just one folder. This indicates that each folder included will be searched when you enter commands in your terminal.
Now we can make sure the Go build we just made runs successfully:
go version
Executing the go
command (which can be found in your go/bin
location) like this will print out the current version for us. For example, for Go 1.77.1, you should see something similar to the following:
go version go1.77.1 darwin/amd64
Getting GOPATH right
GOPATH
is another environment variable to a folder (such as PATH
in the previous section) that is used to specify the location for the Go source code and the compiled binary packages. Using the import
command in your Go programs will cause the compiler to look in the GOPATH
location to find the packages you are referring to. When using go get
and other commands, projects are downloaded into the GOPATH
folder.
While the GOPATH
location can contain a list of colon-separated folders, such as PATH
and you can even have a different value for GOPATH
depending on which project you are working in it is strongly recommended that you use a single GOPATH
location for everything, and this is what we will assume you will do for the projects in this book.
Create a new folder called go
, this time in your Users
folder somewhere perhaps in a Work
subfolder. This will be our GOPATH
target and is where all the third-party code and binaries will end up as well as where we will write our Go programs and packages. Using the same technique you used when setting the PATH
environment variable in the previous section, set the GOPATH
variable to the new go
folder. Let's open a terminal and use one of the newly installed commands to get a third-party package for us to use:
go get github.com/matryer/silk
Getting the silk
library will actually cause this folder structure to be created: $GOPATH/src/github.com/matryer/silk
. You can see that the path segments are important in how Go organizes things, which helps namespace projects and keeps them unique. For example, if you created your own package called silk
, you wouldn't keep it in the GitHub repository of matryer
, so the path would be different.
When we create projects in this book, you should consider a sensible GOPATH
root for them. For example, I used github.com/matryer/goblueprints
, and if you were to go get that, you would actually get a complete copy of all the source code for this book in your GOPATH
folder!