Last Updated: February 25, 2016
·
508
· epxtn

OK Go!: Installing Go on OS X

Recently, I attended an Intro to Go Workshop in Portland, OR led by Kelsey Hightower. It was an excellent introduction to Go. I don't have a lot of experience with compiled languages, so I was interested.

I thought I would get ahead of the game and install Go before the workshop. I didn't want to be the guy fumbling over installation the during the session. I installed the binary package from the Go website, only to realize there was more to the installation once I got to the workshop.

In addition to installing the binary I'll show you a few additional steps you'll need to take before you are up and running.

Install the Binary

To get started install the binary distribution for your machine from the Go downloads page.

Go installs itself at /usr/local/go.

Once the binary package is installed, you have access to the Go tool. If you open your terminal window and type go help, you should see the list of Go commands.

Create a Workspace

Now you need a workspace. On the Go website these steps are located in the How to Write Go Code section. This is the part I didn't take the time to read when I did the installation. Go code must live in a workspace.

Decide where you want your Go code to live. Go gives the example of putting a go folder in your home directory, but you any path you want for your workspace. For the purposes of this post we'll use the home directory.

$ mkdir -p $HOME/go

Now that you have a folder for your workspace, you'll need three directories inside it -- one for executable binaries, one for packages and another for your source code.
$ cd $HOME/go $ mkdir bin src pkg

Go is setup to work with version control from the jump. Your src directory can contain directories for multiple repositories. Create a github.com directory in the src directory. Replace $USERNAME with your github username.

$ mkdir -p $HOME/go/src/github.com/$USERNAME

Set the Environment Variables

We have to tell Go about the workspace. Let's set the GOPATH. If you didn't create it in the home directory, use the path to where you created your workspace. Add the following to your ~/.bash_profile

$ export GOPATH=$HOME/go
$ export PATH=$PATH:$GOPATH/bin

Type go env in your terminal, and you should see your workspace path assigned to GOPATH.

Install Mecurial

Using the Go tool, the go get command can be used to compile and install packages and dependencies. In order for this to work, you will need to install Mercurial. The Go project uses Mercurial for version control. Download the binary package installer for your system.

Test Your Install

Let's create a go program to test the install. Remember to change $USERNAME to your github username.

$ mkdir $GOPATH/src/github.com/$USERNAME/addition
$ cd $GOPATH/src/github.com/$USERNAME/addition

Create a file for your program addition.go.

$ touch addition.go

Add the following code to the file:

package main

import "fmt"

func addInts(x, y int) int {
        return x + y
}

func main() {
        sum := addInts(2, 2)
        fmt.Printf("Theo! 2 + 2 is %v\n", sum)
}

Now build and install the file with the go tool. If you are still in the directory build and install with
$ go install

If you are not in the same directory as the program use
$ go install $GOPATH/src/github.com/$USERNAME/addition

Now you can run
$ $GOPATH/bin/addition

You should see the output
Theo! 2 + 2 is 4

Pat Yourself on the Back

You now have a working Go installation!