Create iOS and Android Programming with Go

View: 888    Dowload: 0   Comment: 0   Post by: hanhga   Category: iPhone OS   Fields: Other

9 point/4 review File has been tested

Next install the GoMobile tool which compiles and runs pre-existing Go applications for Android and iOS:

go get
gomobile init

We will be referring to the example applications shipped with the gomobile package inGoLangInstalldir/src/ If you don’t have them installed, fetch them with this command :

go get -d

Build and Install a Native Go Application

For many application use cases, compiling Go to a native application and ignoring platform libraries and interfaces may be fine. If so it’s simple to compile your existing Go code, with a subset of functionality available. This includes:

  • App control and configuration
  • OpenGL ES 2 bindings
  • Asset management
  • Event management
  • Experimental packages include OpenAL bindings, audio, font, sprite and motion sensors

We will be using one of the pre-existing gomobile examples to step through the process, but these can be replaced with your own project files.


Build an Android APK

gomobile build -target=android

Deploy to a Device

gomobile install


Build an iOS IPA

gomobile build -target=ios

Deploy to a device

There is no equivalent command for deployment on iOS as on Android, so after creating the app above you will have to follow your own personal favorite way of copying it to a device or emulator, such as the ios-deploy tool.

For something a bit more exciting, also try the steps above with the

Let’s take a look inside the audio example (I wont reproduce the complete code here) and you don’t need to be overly familiar with GoLang (I’m not), this is more about looking at what is possible.

Firstly you see a set of import statements:

import (

If you dig around the folders and files that are imported in GoLangInstalldir/src/* you will notice the various Java and Objective-C files that are compiled with your code.

Digging further you will see references to some of the classes imported and used in the code, such asapp and glctx.

Going Native

We can code in Go and build a compact and optimized native app, but it’s not very ‘native-like’ right now, as all the required support libraries are only available in Java and Objective-C / Swift. How can we improve this experience?

The Go Mobile team have created another option, using go packages (your applications) inside a native application. Perfect for sharing some common Go code and binding to native code. It’s easy to get started, but may be complex to maintain in the long run.


If you’re using Android Studio, import the reference project fromGoLangInstalldir/src/ and open the build.grade (Module: hello)file to update your GOPATH and GO paths, here are mine (I installed GoLang with Homebrew):


Sync Gradle and the application is then deployable to an emulator or device.

Let’s look at the Java and Go code:

package org.golang.example.bind;

import android.os.Bundle;
import android.widget.TextView;

import go.hello.Hello;

public class MainActivity extends Activity {

    private TextView mTextView;

    protected void onCreate(Bundle savedInstanceState) {
        mTextView = (TextView) findViewById(;

        // Call Go function.
        String greetings = Hello.Greetings("Android and Gopher");

package hello

import "fmt"

func Greetings(name string) string {
    return fmt.Sprintf("Hello, %s!", name)

The go file is imported with import go.hello.Hello and the Greetings function within it accessible in the Java file via Hello.Greetings. With not too many extra steps, bindings between go functions and native UI elements are possible.


Binding an iOS application to Go requires different steps. First execute the following commands:

cd GoLang_Install_dir/src/
gomobile bind -target=ios

This will create a framework bundle called Hello.framework in the current folder that we can use in our project.

Open the sample iOS project found atGoLangInstalldir/src/ in Xcode and dragHello.framework into the project, checking Copy items if needed. This should result in the following folder structure:

Go in Xcode

Build and run the application which, much like the Android app, calls Go methods into Objective-C code.

Let’s look at the code:

#import "ViewController.h"
#import "hello/Hello.h"  // Gomobile bind generated header file in hello.framework

@interface ViewController ()

@implementation ViewController

@synthesize textLabel;

- (void)loadView {
    [super loadView];
    textLabel.text = GoHelloGreetings(@"iOS and Gopher");


#import "hello/Hello.h" imports the framework file generated earlier andtextLabel.text = GoHelloGreetings(@"iOS and Gopher"); calls the function it exposes to set a label variable.

It’s possible to use the same auto-generated Objective-C framework file in a Swift based project and then use something like:

let msg = Hello.GoHelloGreetings("gopher")


Create iOS and Android Programming with Go

Create iOS and Android Programming with Go Posted on 25-02-2016  Next install the GoMobile tool which compiles and runs pre-existing Go applications for Android and iOS 2.25/10 888


To comment you must be logged in members.

Files with category

File suggestion for you
File top downloads - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. Develope by Vinagon .Ltd