Go is now installed, but in order to use the tools we must ensure it is properly configured. To make calling the tools easier, we need to add our go/bin
path to the PATH
environment variable.
On Unix systems, you should add export PATH=$PATH:/opt/go/bin
(make sure it is the path you chose when downloading the source) 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 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 the value printed contains the correct path to your go/bin
folder, for example, on my machine it prints as:
/usr/local/bin:/usr/bin:/bin:/opt/go/bin
Now we can make sure the Go build we just made runs successfully:
go version
Executing the go
command (that can be found in your go/bin
location) like this will print out the current version for us. For example, for Go 1.3, you should see something similar to:
go version go1.3 darwin/amd64
GOPATH
is another environment variable to a folder (like PATH
in the previous section) that is used to specify the location for Go source code and 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/stretchr/powerwalk
Getting the powerwalk
library from Stretchr
will actually cause the following folder structure to be created; $GOPATH/src/github.com/stretchr/powerwalk
. 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 powerwalk
, you wouldn't keep it in the GitHub repository of Stretchr
, 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!
52.15.74.25