First steps with Go in Ubuntu Karmic

It’s all over the Internet today, Google has released a new programming language called Go. As of what I have been able to see so far, Go can be described as a systems language which aims to leverage Python’s expressiveness into the grounds of compiled languages as C++. Also, Go is both fast to compile and to execute. You can see some pretty impressive footage of Go’s compiler speed in this video.

A good starting point for Go is that it’s Open Source, so you can start playing with it right now (as long as you are a linux or mac user). That’s what I have done, and I’m posting my first steps under Ubuntu Karmic in case it can help anybody.

Installation

Go’s compiler and tools are currently distributed in source, so you will first need to ensure that you have certain compiling packages installed in your system so you will be able to compile Go’s tools. Also, Google uses Mercurial for revision control, so you will need it for checking out Go’s tree.

Execute the following in a shell:

sudo apt-get install mercurial bison gcc libc6-dev ed

And you will have all what you need to fetch and build Go’s sources. We will install it in a clean way so everything is under one directory and maintenance and an eventual removal is easy. First, you need to create the directory where everything Go-related will go:

mkdir ~/golang

Now, you need to edit your .bashrc file. It’s located at the root of your home directory. Add the following lines at its end (you will have to change the GOARCH variable to amd64 if you are running the 64bit version):

# Go Installation
export GOROOT=~/golang
export GOOS=linux
export GOARCH=386
export GOBIN=${GOROOT}/bin
PATH=${PATH}:${GOBIN}
export PATH

With that in place, open a new terminal so your .bashrc gets reloaded. Within that terminal, check out Go’s sources with Mercurial by typing:

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

And once it finishes checking out, create the bin directory and compile with:

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

If all went right, you are now ready to start using Go. You may check by typing 8g or 6g (for 386 and amd64 respectively) and seeing if it executes the compiler. I must say that the installation worked flawlessly for me following the instructions in Go’s homepage, so up to this point this guide is just an adaptation taking care of Ubuntu’s specific parts.

Keeping Go up to date

Go’s development is quite lively. Roughly 10 minutes past installing, I checked the repository and there were 15 new updates. So it’s a good idea to have a recent version of Go installed (specially if you are going to do things as bug reporting).

You can keep your installation updated by executing:

cd $GOROOT
hg pull -u
cd src
./all.bash

Also, if you would want to remove Go from your system, it’s as easy as deleting the $GOROOT directory and removing the added lines from your .bashrc file.

Start!

OK, now you are ready to start experimenting with Go. You can write the standard Hello World program as:

package main
 
import format "fmt"
 
func main() {
	format.Printf("Hello World!\n")
}

And compile, link & run with:

8g hello.go
8l -o hello hello.8
./hello

As side note, if you are interested in a system wide installation (instead of per-user, as I just described), change your $GOROOT declaration to somewhere where every user has read permissions (like /opt/golang or /usr/local/golang), and edit /etc/environment instead of your user’s .bashrc. Also, you will have to execute most commands with sudo as you will need write permissions under $GOROOT.