The following was translated from the original Japanese article posted on July 23, 2015.


Getting Started


@tenntenn reporting in.


Go 1.5 is on the verge of being released, and I’m curious to know which new feature everyone is looking forward to most. With concurrent GCs and the ability to create shared libraries, the new version of Go is looking pretty sweet. However, even amidst all this Christmas-like excitement, I find myself particularly attracted to the Go Mobile update introduced in Go 1.4.


Go Mobile is a project that provides an array of tools for creating mobile apps in Go. With the advent of Go 1.5, developers can create app builds for iOS and enjoy the enhanced support provided for Android. As of July 19, 2015, the latest master branch already includes functionality for creating iOS builds. The Go Team also released an APL-like interpreter, known as Ivy, for iOS and Android.


The other day, I released an app on Google Play as a test for a Lightning Talk (LT) Timer I used at the Go Conference 2015 Summer LT Conference. The app is called GoFun, and here’s the source code. I tried testing my limits by creating the app without using Java. It’s written completely in Go.


I run across articles about apps written in a mix of Java and Go all the time, but I hardly ever see articles about programs written exclusively in Go. I found some interesting gems while researching for this project, and I’ll be sharing them here on this blog. For my first article, I’ll explain what Go Mobile is and how to install it.


The details and steps contained in this article are current as of July 19, 2015. Go Mobile is still under development, and it’s changing radically almost every day. This means that methods for creating builds, package names, and the ways different libraries and tools are used may change and evolve over time.


All the testing done for this article was conducted using Mac OS X Yosemite (10.10.3) and Nexus 9 (Android 5.1.1). No testing was done on any other OSes or Android devices. Though it seems like Go Mobile runs on Linux, it doesn’t run on Windows...yet.


Installing Go Mobile


You need Go 1.5 in order to use the latest version of Go Mobile. Either download Go 1.5 or build your own from the source code. You’ll need Go 1.4 in order to create the build for Go 1.5.


Once you’ve finished installing Go 1.5, it’s time to “go get” gomobile command.


$ go get golang.org/x/mobile/cmd/gomobile


gomobile command will be installed inside $GOPATH/bin. If $GOPATH/bin isn’t included in $PATH, be sure to add it in. Now you’re ready to use gomobile command.


$ gomobile -h
Gomobile is a tool for building and running mobile apps written in Go.

To install:
....


Next, we’re going to run gomobile init. This command will install the necessary components so you can start using Go Mobile. It looks like everything you need for cross-compiling builds for mobile devices, such as Go Toolchain, Android NDK, and OpenAL (libopenal) will be installed for you. In my case, android-ndk-r10d was installed inside $GOPATH/pkg/gomobile/. Since it’ll take a long time to install, it might be wise to tack on a -v and check the progress every once and a while.


$ gomobile init -v


Writing Mobile Apps Purely in Go



You can create mobile apps in Go with Go Mobile using either of the following two methods.


- Call functions written in Go from Java (Android) or Objective-C (iOS)

- Use OpenGL or OpenAL to write apps in Go


Go Mobile was not designed to provide wrappers for all the APIs provided by Android and iOS. Just like GUI and other Android and iOS app development tools, it uses Java and Objective-C, and lets Go take care of the processes it’s best equipped to handle. (I haven’t looked into whether or not you can make calls through Swift. Sorry!)


Go Mobile allows you to call OpenGL and OpenAL functions. Even if you’re not using a standard OS UI for your game, you can still use these tools to develop your app. However, the features it provides are still on the low-end of the functionality spectrum. As you can tell from the fact that many of the packages have been placed inside exp, a lot of the features are still in the experimental phase of the implementation process.


You can use almost all of the features that come with Go’s standard packages from your Go Mobile app.


Making Your Sample “Go”


You’ll find a directory entitled example in the Go Mobile repository. Inside this handy directory, you’ll find a handful of excellent samples to get you going with Go Mobile. Let’s take a closer look at the most basic of all these samples: example/basic.


First, let’s try running it on our Mac. Go to the directory with the sample inside and execute the go run command.


$ cd $GOPATH/src/golang.org/x/mobile/example/basic/
$ go run main.go


If you’re doing it right, you should see a green triangle positioned atop a sea of red, much like the screenshot below. You should be able drag the green triangle across the screen.


basic


Now that we’ve confirmed we can move the triangle around on our Mac, let’s make sure we can do the same thing on our Android device. Use the gomobile build command to create a mobile app version of the build. This command creates an APK file by default. You can also create an APP file for iOS devices by using -target ios. If you create an IPA from the APP, you can probably move the triangle around on the iOS device. However, I can’t make any promises because I wasn’t able to generate an IPA file with the platform I was using.


$ cd ~/Desktop #You can put it anywhere, really
$ gomobile build golang.org/x/mobile/example/basic


Inputting the command shown above will generate a basic.apk file in your current directory. Use adb install to install the APK on your Android device.


$ adb install basic.apk


If everything’s going smoothly, you’ll see something similar to the screenshot below on your Android device!


basic_android


I used gomobile build in the example above, but if you use gomobile install, you can use adb install after you generate the build. This does everything for you automatically right up to the point where it installs the app on your Android device. The adb command is required to use gomobile install. Get your Android SDK development environment set up and ready to go, and then run the adb command through the path. Be careful, though—if the build target for the gomobile install command isn’t an android device, your app won’t run at all.


$ gomobile install golang.org/x/mobile/example/basic


I hope you were able to glean something useful from this article! Certainly, watching your app written entirely in Go running on your Android device is its own reward. Next time, we’ll take a look at another sample to check out how Go Mobile’s sprite package works.