Installing and Running Google Go on Mac OS X

A while ago Google gave us a brand new language, Go. In the last couple of years I really haven’t heard much about it, but I decided to finally install it and give it a try. This tutorial will guide you step-by-step on how to install Google Go and start writing your own applications.

Go’s website has very good instructions on how to install it, and this tutorial will take many steps directly from there. Their website has instructions for all the major platforms, however this article will be specific to Mac OS X running on a 64 bit Intel processor.

Google does not distribute the binaries, which means you’ll have to build it for your own platform.

Setup Environment Variables

When you build Go, the compiler is going to need to know a few things.

export GOROOT=$HOME/go
export GOARCH=amd64
export GOOS=darwin
export GOBIN=$HOME/go/bin

$GOROOT is the path to where you’ll download the Google Go source code. I left mine where Google suggested, which is $HOME/go (/Users/[username]/go).

$GOARCH is the architecture you’re building for. The options are amd64, 386, or arm. The name is slightly misleading, but amd64 works for all 64bit x86 processors, and is what you’ll want to choose for Inlet 64 bit processors.

$GOOS is the operating system. The options here are darwin (for Mac), freebsd, linux, and nacl.

$GOBIN is where the output from the build will be placed. You’ll have to create this folder before building Go.

Grabbing The Go Source

Go uses Mercurial for version control. I didn’t have Mercurial installed, so I had to install it with this command.

$ sudo easy_install mercurial

After you have Mercurial installed, fetch the repository to your Go root.

$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT

Building Go

This step assumes you have gcc and a bunch of other crap installed. The simplest way to get these is to just install Xcode. If you’re a developer on a Mac, however, chances are you’ve already got it installed.

$ cd $GOROOT/src
$ ./all.bash

When the build is complete, you’ll see something that looks like this:

2 known bugs; 0 unexpected bugs

The known bugs are expected and the number may vary based on the current release of Go and your architecture.

Once this is done, a compiler and a linker has been placed in the bin folder. Time to add that folder to the $PATH environment variable.

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

Writing Some Go Code

With your favorite text editor, create a helloworld.go file and add some Go code to it.

package main

import "fmt"
import "time"

func main() {
        var currentTime = time.LocalTime()
        fmt.Printf("The current time is: %s\n", currentTime.String())
}

The compiler and linker will be called 8g and 8l. We need to now compile and link our application.

$ 8g helloworld.g
$ 8l helloworld.8

After it builds and links without errors, it’s time to run our program.

$ ./8.out

Which outputs:

The current time is: Mon Jun 14 22:47:08 EDT 2010

And there you have it. If you own a Mac and are interested in messing with Google Go, this tutorial should get you started. Go has a pretty good sized framework that should let you do a lot of interesting things. Google uses Go internally for production systems, which I guess gives it some credibility as a mature language. Hopefully Go can gain some mainstream momentum in the future, and I’ll definitely be watching it grow.

Leave a Reply

Your email address will not be published. Required fields are marked *