Ready to Go? Compile it

Go 1.6 will be released in few days. In preparation let’s see how we can compile it from source.

Go was conceived while its creators were waiting for their C++ code to compile. They have enough time to complete the full specs of Go before that complied finished [1]. We can check how much compiling Go itself will take.

To compile Go you need a running version of it. Download a binary package of your choice and follow the instructions. I am using Mac so if you are on GNU/Linux just adjust my commands to fit your OS.

You can find the binary packages in website.

$ cd ~
$ mkdir go
$ cd go
$ curl -o go.tar.gz
$ tar --gzip -xf go.tar.gz
$ mv go goroot
$ rm go.tar.gz

Now you have the binaries ready. You need to set two environment variable to make it functional GOROOT and PATH

$ export GOROOT=~/go/goroot
$ go version
go version go1.5.3 darwin/amd64

Last command shows we have a usable Go installation.

Compiling the new version from the source:

$ cd ~/go
$ git clone go-src
$ cd go-src
$ find . -name '*.go' -exec cat {} \; |cat -n|tail -1
932694 }

It is around 900K.

Notice that I will tell the script to compile for Mac (darwin) and amd64. You can find the full list in Go docs but if you are on linux you most probably need, “linux” and “amd64”.

$ cd src
$ time GOROOT_BOOTSTRAP=~/go/goroot GOOS=darwin GOARCH=amd64 ./bootstrap.bash
real 1m17.861s
user 2m35.443s
sys 0m18.521s

Damn it! I didn’t even have enough time to make my espresso!

Let’s switch to our compiled version. Go compile process will create a directory and one tar file in ~/go. I will delete the tar file and use the new directory.

$ cd ~/go
$ rm go-darwin-amd64-bootstrap.tbz
$ rm -rf goroot
$ mv go-darwin-amd64-bootstrap goroot
$ go version
go version devel +aa22c42 Tue Feb 16 22:02:56 2016 +0000 darwin/amd64

That was all. You have the latest Go compiler and all its standard libraries ready.

In future, anytime you want the latest version of go, just pull the latest changes and recompile it.

$ cd ~/go/go-src
$ git pull
$ cd src
$ time GOROOT_BOOTSTRAP=~/go/goroot GOOS=darwin GOARCH=amd64 ./bootstrap.bash

Now that you have latest version of Go, you just need to start a simple project and try it. Notice you still need to set one more environment variable that is GOPATH. Go will use that path to find and install libraries that your app needs.

If you like to learn how to use Go check these links:

Have fun,

[1] This is just an exaggeration but it has root in truth.