There is no question that the trend today in mobile app design is to get every possible language onboard for creating mobile applications. This is sort of the case with GoMobile. Far from being originally intended to create mobile apps, Go or GoLang, was originally created at Google in 2007. Go has true concurrency capabilities, which can lend itself well to any programming task, certainly mobile app creation. The first thing you need to do to follow along with this blog is get the GoLang binaries on your machine. Although there is a GCC tool to compile Go, I would strongly recommend using the Go tools.
I like Go because it is powerful, safe, and it feels new. It may simply be a personal preference, but I think Go is a largely underrated language. This blog will assume a minimum understanding of Go; don’t worry so much about the syntax, but you will need to understand how Go handles projects and packages. So to begin, let’s create a new folder and specify it as our $GOPATH bash variable. This tells Go where to look for code and where to place the downloaded packages, such asGoMobile. After we specify our $GOPATH, we add the bin subdirectory of the $GOPATH to our global $PATH variable. This allows for the execution of Go tools like any other bash command:
$ cd ~ $ mkdirGoMobile $ export$GOPATH=~/GoMobile $ export PATH=$PATH:$GOPATH/bin
The next step is somewhat more convoluted. Today, we are getting started with Android development. I choose Android over iOS because GoMobile can build for Android on any platform, but can only build for iOS on OSX. In order for GoMobile to be able to work its magic, you’ll need to install Android NDK. I think the easiest way to do this is through Android Studio. Once you have the Android NDK installed, it’s time to get started.
We are going to be using an example app from our friends over at Go today. The app structure required for Go-based mobile apps is fairly complex. With this in mind, I would suggest using this codebase as you begin developing your own apps. This might save you some time. So, let’s first install GoMobile:
$ go get golang.org/x/mobile/cmd/gomobile
Now, let’s get that example app:
$ go get -d golang.org/x/mobile/example/basic
For the next command, we are going to initialize GoMobile and specify the NDK location. The online help for this example is somewhat vague when it comes to specifying the NDK location, so hopefully my research will save you some time:
$ gomobileinit -ndk=$HOME/Library/Android/sdk/ndk-bundle/
Obviously, this is the path on my machine, so yours may be different:however,if you’re on anything Unix-like, it ought to be relatively close. At this point, you are ready to build the example app. All you have to do is use the command below, and you’ll be left with a real live Android application:
$ gomobile build golang.org/x/mobile/example/basic
This will build an APK file and place the file in your $GOPATH. This file can be transferred to and installed on an actual Android device, or you can use an emulator. To use the emulator, you’ll need to install the APK file using the adb command. This command should already be onboard with your installation of Android Studio. The following command adds the adb command to your path(your path might be different, but you’ll get the idea):
At this point, you ought to be able to run the adb install command and try out the app on your emulator:
adb install basic.apk
As you will see, there isn’t much to this particular app, but in this case, it’s about the journey and not the destination. There is another way to install the app on your emulator. First, uninstall the app from your Android VM. Second, run the following command:
gomobile install golang.org/x/mobile/example/basic
Although the result is the same, the second method is almost identical to the way that the regular Go builds applications. For consistency’s sake, I would recommend using the second method.
If you’re new to Go, at this point, I would recommend checking out some of the documentation. There is an interactive tutorial called A Tour of Go. I have found this tutorial enormously helpful for beginning to intermediate needs. You will need to have a pretty deep understanding of Go to be an effective mobile app developer. If you are new to mobile app design in general, e.g., you don’t already know Java, I would recommend taking the Go route. Although Java is still the most widely used language the world over, I myself have a strong preference for Go.
About the Author
Erik Kappelman is a transportation modeler for the Montana Department of Transportation. He is also the CEO of Duplovici, a technology consulting and web design company.