Go: "Hello, Android!"

Chatting to bradfitz this morning, he mentioned he was going to try using Go to do some scripting-style tasks on the Android platform.

It turns out getting Go running on an ARM-based Android phone is easy. First, build Go for linux/arm:

~/go$ hg update -r release
22 files updated, 0 files merged, 1 files removed, 0 files unresolved
~/go$ GOOS=linux
~/go$ GOARCH=arm
~/go$ cd src
~/go/src$ ./all.bash

For testing purposes, we’ll use the most trivial of all Go programs:

package main

func main() {
    println("Hello, Android!")

Compile and link it (the ARM compiler and linker is called 5g/5l):

~$ 5g hello.go && 5l hello.5

This will produce a binary called 5.out, that we now need to transfer to the phone itself. To do this, enable USB debugging on the phone, plug it into your computer, and use the ‘adb’ tool from the Android SDK to upload the file:

~$ adb push 5.out /data/local/hellogo
1036 KB/s (155648 bytes in 0.146s)

Our binary will now be at /data/local/hellogo. Now we need to open a shell on the phone, and execute the program:

~$ adb shell
$ data/local/hellogo
Hello, Android!

9515 views and 10 responses

  • Jul 10 2010, 2:05 AM
    bill responded:
    Cool. But how do I get go installed on my Android in the first place. I mean, can I get hg on a phone?

    I am confused....

  • Jul 10 2010, 6:11 PM
    Andrew Gerrand responded:
    Hi Bill, I should have been more explicit. I built the linux/arm compilers on my OS X system. I then built the hello world program there, and transferred the binary to my phone. This practice is known as cross-compilation.

    There's not much point in running the Go tool chain on the phone, unless you wanted some kind of portable development environment. ;-)

  • Jul 12 2010, 10:09 PM
    anthony baxter responded:
    What? You didn't try and get the compiler and linker running on Android? Booooo! :)
  • Jul 26 2010, 12:21 PM
    jay responded:
    Is there potential in it to be able to write android apps/programs in go in the future? or is there some kind of thing limiting that? (besides the need for "driver" type classes)
  • Jul 27 2010, 1:54 AM
    Andrew Gerrand responded:
    The big hurdle for writing Android apps in Go (and I'm no Android expert) is linking it into the Android runtime (which runs on the Dalvik VM). As I understand it, it would be possible to write full-screen apps in Go (like games), but writing stuff that hooks into the greater Android ecosystem would be a lot of work.
  • Aug 13 2010, 9:04 AM
    Joseph Stewart responded:
    For those of you who are running on non-Android ARM targets and getting an "Illegal instruction (core dumped)" when running, rebuild the compiler after doing "export GOARM=5"
  • Aug 13 2010, 8:23 PM
    Andrew Gerrand responded:
    To expand on Joseph's suggestion: The default value for GOARM is 6, which targets ARMv6 cores that have more efficient synchronisation primitives. GOARM=5 will build compilers that produce binaries that use a backwards-compatible but less efficient instruction set.
  • Oct 13 2010, 4:31 AM
    Roger responded:

    I'm new to Android development, but wouldn't it be possible to write some parts of Android apps using the Android NDK in Go and then do the GUI using Java? Or the NDK is only designed to work with C/C++ code?

  • Dec 28 2010, 9:25 PM
    Andrew Gerrand responded:
    The NDK is written in C so it is naturally well-suited for writing C/C++ applications. With that said, it would be possible to link a Go program against the NDK using something like cgo or SWIG, but that would be a lot of work.
  • Apr 29 2013, 8:09 PM
    Matías Insaurralde responded:
    This looks great. Can you recommend a book about Go? I'm currently reading "Learning Go".