Installing the Go SDK

Here's a quick refresher in case you're at a new machine or haven't installed Go in a while. If you've already set up Go and have done a few tutorial programs feel free to skip this chapter. Its sole advantage over the Go documentation is that it suggests a typical build configuration, which is a little hard to find in the Golang tutorials. This section shows how to:

  • Obtain and Install Go
  • Verify the installation
  • Configure the Go build environment
  • Write and run a quick test program

Obtaining the Go SDK

  • Visit the Downloads page for Go and choose the binary installer for your platform.
  • Accept all defaults. Go is opinionated and it will make your life easier.

The Go installer should add Go binaries to the environment's PATH. Often you must restart your command prompt to get the new path.

Making sure Go was installed

  • If you are using a terminal program/command prompt close it, then restart.
  • Run go version to get the version. You should see the version number displayed as shown below:
$ go version
go version go1.4.2 darwin/amd64

If you see something like this, the installation failed, or you forgot to close your terminal and restart.

'go' is not recognized as an internal or external command,
operable program or batch file.

Or

-bash: go: command not found

Creating your source directory

Now it's time to determine where your source code go.

You'll create a directory tree someplace away from $GOPATH because when a new version of Go comes out, you reinstall by essentially deleting the existing Go directory and replacing it with the new version's directory tree.

The easiest way is to create a subdirectory named go in your home documents directory.

Creating your working directory, Windows edition

You can create the go directory and its suggested child directory go/src with this command. Using %HOMEPATH% automatically places it in the user's home directory.

  • Create the subdirectory and change to it like this:
# Creates a directory in user's primary 
# directory named go, and a subdirectory
# named go/src. Upon completion, 
# Makes that the current directory.
> md %HOMEPATH%\go\src && %HOMEPATH%\go\src

Creating your working directory, MacOS/OS X and Linux version

You can create the go directory and its suggested child directory go/src with this command:

  • Create the subdirectory and change to it like this:
# Creates a directory in user's primary 
# directory named go, and a subdirectory
# named go/src. Upon completion, 
# Makes that the current directory.
$ mk -p $HOME/go/src && cd $HOME/go

Adding your $GOPATH, OS X and Linux version

Now let Go know about the directory you just created, so it will know where to deposit executables created by the compiler, find your libraries, and so on. To do this you'll be updating environment variables so Go can find the paths.

To do this you must append the following lines to the end of either the file ~/.bash_profile (if you want this change only to your private working environment) or etc/profile (if you are working on a shared machine with other Go developers).

# The following lines need to be added 
# to the environment:
GOPATH=$HOME/go 
PATH=$PATH:$GOPATH/bin
  • Here's how to append those lines without using a text editor:
$ echo "export GOPATH=$HOME/go" >> ~/.bash_profile
$ echo "export PATH=$PATH:$GOPATH/bin" >> ~/.bash_profile

You must reload the command shell at this point. You can either log out of your current computer session, then log back in or do this from the command line:

$ exec -l $SHELL

Optional: Add a github repository directory

  • If you're a GitHub user, add a directory for your repos under your $GOPATH directory. Obviously replace your-github-username with your username on GitHub:
$ mkdir -p $GOPATH/src/github.com/your-github-username

Making sure your PATH variables were set

  • Close your terminal program/command prompt, then reopen it.

This ensures the environment variables are available.

  • Search the output of env to ensure the variables were set.
$ env | grep PATH

The code above sends the output of env, which lists all environment variables to the standard output, to a case-sensitive search program (grep), so that the matching text must contain the word PATH in it.

You should seee something like:

PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/mysql/bin/:/bin
GOPATH=/Users/tom/go/

Your output will vary, and will doubtless be more verbose than shown above. Just make sure you see something like go/bin on the PATH and something matching your configuration for the GOPATH too.

Write a quick test program

Finally. We'll write a short Go program to ensure the compiler and build system work properly.

  • Start your text editor:
# Replace vi with whatever text editor you prefer
$ vim $GOPATH/hello.go
  • Write and save this program:
package main

import "fmt"

func main() {
    fmt.Println("hello, world.")
}
  • Compile it, run it, and observe the output all in one step:
$ go run $GOPATH/hello.go
hello, world.

Resources

How to Write Go Code shows recommendations for your workspace and code organization.

results matching ""

    No results matching ""