Saturday, July 10, 2010

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!

Posted via email from

No comments: