Quick Glimpse at Go 1.5


I’ve been playing around with Golang for a few months now and I have to conclude that it is indeed a practical, well thought out language that is also fun to use. If you haven’t heard about or tried it yet, head over to tour.golang.org right now. There you can try the language right inside your browser, hassle free, no installation required. Though there’s something compelling about compiling things on your own machine. Actually, I have to admit that I was not that “into” Go when I first saw it and it took me about a year to come around and try it out again, but this time focusing more on the why, not the how. What really spiked my interest was watching these videos where developers from the Go team explain their thinking behind the decisions made and present their view of a modern, pragmatic programming language and tooling as a big part of it all. If you have some free time I encourage you to watch them. I do believe that there’s something to be gained here even if you choose not to learn Go. At least watch the talk by Rob Pike.

Some say that Go is a system programming language, though in reality it’s more of a general purpose one. But in the end there is no single programming language that will fit all of the criteria for all the people, that’s why there are so many of them. Though the way I see it - not many languages have a justifiable reason for their existance. When you dig into the history of some languages quite a few of the currently popular ones started out as some side-project, hack or a pre-processor for another language as a way to avoid using some unwanted other language. (The language names have been omitted to protect the innocent).

The thing I personally have been looking forward to this year is the release of the Go 1.5 which should be quite soon, I believe the 1 of August is the current due date. The cool thing is that beta is already out and can be downloaded today from the golang page. Among various improvements to the compiler and a lot of focus on Garbage Collection, my favorite feature is the new crazy simple way of cross compiling i.e. building statically linked, zero dependency binaries for platforms other than that on which you’re writing the code. Once you have your GOPATH environment variable setup, the build process is already as simple as doing a go build inside your project directory. But now you can specify the target Operating System and Architecture, so on a linux machine you can do the following

$ GOOS=windows GOARCH=amd64 go build .

this will produce a 64-bit Windows executable file. Similarly you can specify any other supported platform. And you can even create binaries for Mac OS X, now you don’t even need an Apple computer for that.

But wait, there’s more. What if I told you you can now develop Android and iOS applications with Go. Well actually I don’t think it’s meant as a replacement of the current development environments for mobile apps, but rather as a supplement for performance sensitive parts of the application (think NDK for Android). The iOS part is not yet ready at the time of writing, but you can go try the Android part right now. All you need to do is install a couple of packages and initialize the environment.

$ go get -u golang.org/x/mobile/cmd/gomobile
$ go get -u golang.org/x/mobile/cmd/gobind
$ gomobile init

Now you’re ready. There are a few sample apps included in the gomobile package. To try one out just do the following

$ cd $GOPATH/src/golang.org/x/mobile/example/sprite
$ gomobile build .

Voila, you should see a sprite.apk file that is the result of this build. You can now install it onto your Android phone or an emulator. If you’re going to try it on an emulator make sure that the emulator’s system image is for ARM architecture since that’s the target architecture for the library. Will need to investigate if there’s a way to build it for different architectures, because right now this app wouldn’t run on an Atom-based Android device. The quickest way to install the app (presuming you have Android SDK) is to run

$ adb install basic.apk

And to simplify things further you can run the application based on the package name which can be extracted from the .apk itself.

$ run-apk.sh basic.apk

Here’s the source for the run-apk.sh script:

#!/bin/bash
pkg=$(aapt dump badging $1|awk -F" " '/package/ {print $2}'|awk -F"'" '/name=/ {print $2}')
adb shell monkey -p $pkg -c android.intent.category.LAUNCHER 1

Admittedly this wasn’t a changelog type of overview of the new version of Go, but I just wanted to quickly mention a couple of cool new things you’ll be able to do with Go 1.5, which I’m excited about. Hopefully some of this sparked enough interest to motivate you to go and check out the language for yourself. Happy exploring :)