Sunday, May 2, 2010

Installing golang on Ubuntu Linux

In your $HOME/.bashrc, set the necessary environment variables:
export GOROOT=$HOME/go
export GOOS=linux      # target operating system
export GOARCH=386      # target compiling architecture

Note that with golang, you are always "cross-compiling".  So the values for $GOOS and $GOARCH are not necessarily corresponding to your Linux box.

Make sure you have the tools needed to build the golang compilers from source:
sudo apt-get install python-setuptools python-dev build-essential gcc mercurial

Then get the golang source code using mercurial:
cd $HOME && hg clone -r release $GOROOT

Now let's build golang from the downloaded source code:
cd $GOROOT/src && ./all.bash

The golang executables will be built and placed into $HOME/bin (you can customize this by setting the environment variable $GOBIN). This will also run some tests on the build. In the end you should see something like:
--- cd ../test
N known bugs; 0 unexpected bugs

Here, N changes from version to version of golang. That's it! So now it's time for the customary "Hello, World". Open your favorite text editor and create a simple text file called hello.go:
package main

import "fmt"

func main() {
    fmt.Printf("hello, world!\n")

Then build our executable (note that we're compiling for linux-386):
8g hello.go
8l hello.8

In the end, you'll see a nice little "hello, world!"

No comments:

Post a Comment