Appendix A
Downloading Go
Visit the Go project’s downloads page1 and select the binary distribution that matches your operating system and processor architecture.
Install the Go tools
Read through the installation guide2 for Linux, Mac OS X, FreeBSD and Windows.
The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install them in a different location. If you do this, you will need to set the GOROOT environment variable to that directory when using the Go tools.
For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
|
Warning:
|
Under Windows, you may set environment variables through the “Environment Variables” button on the “Advanced” tab of the “System” control panel. Some versions of Windows provide this control panel through the “Advanced System Settings” option inside the “System” control panel.
The Go-environment works with a small number of OS environment variables. They are not required for building the Go-environment, but by setting them and thus overriding the defaults the Go compilation environment can be customized. I have set the following:
| Name | Value |
|---|---|
| GOROOT | C:\go |
| GOOS | windows |
| GOARCH | 386 |
Environment Variables
On Windows:
In the same dialog-window: Edit the PATH-variable as follows:
C:\go\bin; ...rest of PATH...
Test your installation
Check that Go is installed correctly by building a simple program, as follows.
Create a file named hello_world.go in some folder (for now) and put the following program in it:
1 package main
2
3 import "fmt"
4
5 func main() {
6 fmt.Println("Hello, world.")
7 }
Then from the folder where you have saved the file hello_world.go run it with the go tool:
$ go run hello_world.go
Hello, world.
If you see the “Hello, world.” message then your Go installation is working.
Go Code Organization
The go tool3 is the standard way to fetch, build, and install Go packages and commands. The go tool requires you to organize your code in a specific way.
Workspaces
Go code must be kept inside a workspace. A workspace is a directory hierarchy with three directories at its root:
-
srccontains Go source files organized into packages (one package per directory), -
pkgcontains package objects, and -
bincontains executable commands.
The go tool builds source packages and installs the resulting binaries to the pkg and bin directories.
The src subdirectory typically contains version control repositories (such as for Git) that track the development of one or more source packages.
The GOPATH environment variable
The GOPATH environment variable specifies the location of your workspace.
To get started, create a workspace directory and set GOPATH accordingly. Your workspace can be located wherever you like. Note that this must not be the same path as your Go installation.
On my Windows computer, I have set GOPATH=C:\go_projects\go. Next I update my system environment variable PATH to include my workspace bin subdirectory i.e. PATH=%PATH%;%GOPATH%\bin;
We need to do the same on *nix or Mac as well.
mkdir $HOME/go_projects/go
export GOPATH=$HOME/go_projects/go
export PATH=$PATH:$GOPATH/bin
My workspace folder structure
C:\go_projects
\---go
+---bin
+---pkg
\---src
Package paths
The packages from the standard library are given short paths such as fmt and net/http. For your own packages, you must choose a base path that is unlikely to collide with future additions to the standard library or other external libraries. If you have a GitHub account at github.com/user, that should be your base path. We will use
github.com/user as our base path. Create a directory inside your workspace in which to keep the source code. I have created the folder C:\go_projects\go\src\github.com\SatishTalim.
|
TipReplace |
Editing a Go program
Go programs are written as plain text Unicode using the UTF-8 encoding. All of Go’s keywords and operators use ASCII characters; however, Go identifiers can start with any Unicode letter followed by any Unicode letters or digits, so Go programmers can freely use their native language.
A Go program
To compile and run a simple program, first choose a package path and create a corresponding package directory inside your workspace:
$ mkdir $GOPATH/src/github.com/SatishTalim/hello
Next, create a file named hello.go inside that directory, containing the following Go code:
1 package main
2
3 import "fmt"
4
5 func main() {
6 fmt.Printf("Hello, world.\n")
7 }
Now you can build and install that program with the go tool:
$ cd $GOPATH/src/github.com/SatishTalim/hello
$ go install
The go tool will only print output when an error occurs, so if these commands produce no output they have executed successfully.
The above command i.e. go install4 compiles, builds the hello executable binary and then installs that binary to the workspace’s bin directory as hello (or, under Windows, hello.exe).
You can now run the program by typing:
$ hello
Hello, world.