Go on Mobile

GopherCon 2015

Hana Kim

Google

Video

A video of this talk was recorded at GopherCon in Denver.

2

Caution

The Go Mobile project is experimental. Use this at your own risk.

While we are working hard to improve it, neither Google nor the Go
team can provide end-user support.

3

Background

Mobile support was frequently requested

Some users built their own Go binaries for Android with cgo + external linking through NDK tool chains

Some Android Apps used Go even before Go 1.4

4

golang.org/x/mobile

Goal: Bring Go to Mobile Platforms

Why?

5

Two ways of using Go

Native Apps

SDK Apps

6

Native Apps

7

Challenge #1: Cross-platform APIs

Work for Android, iOS, and Desktop environments

Provide a rich set of APIs

Follow idiomatic Go style

8

Demo: Hello, Gopher!

This program uses the packages from golang.org/x/mobile repo
There is no Java or Objective-C or C in my code

9

What's available?

golang.org/x/mobile/...

golang.org/x/mobile/exp/...

10

Challenge #2: Build systems

Dealing with

That is not fun!

11

The gomobile tool

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

Simplifies toolchain installation and app deployment

To install the Android/iOS compiler tool chain:

$ gomobile init

To build an Android APK and an iOS app

$ gomobile -target=android build
$ gomobile -target=ios     build

(Demo)

12

SDK Apps

13

Go as a library

Go 1.5 can build Go programs as a library that can be used by non-Go programs

Functions marked with //export cgo annotations are callable.

14

Working with Foreign Languages

Manually mapping data structures and functions between languages is tedious and error-prone!

15

The gobind tool

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

Automates language binding through code generation

Defines the language binding from exported Go APIs; no explicit annotation

Currently supports a subset of Go types

16

Binding Functions, Basic Types & Errors

Go API

package mypkg

func Hello() (string, error) { return "Gopher", nil }

Generated Java API

public abstract class Mypkg {
    public static String Hello() throws Exception { ... }
}

Generated Objective-C API

FOUNDATION_EXPORT BOOL GoMypkgHello(NSString** ret0_, NSError** error);
17

Binding Structs

package mypkg

type Counter struct {
    Value int64
}

func (c *Counter) Inc() {
    c.Value++
}

func NewCounter() *Counter {
    return &Counter{}
}
18

Generated Java API

public abstract class Mypkg {
    public static final class Counter {
        public void Inc() { ... }
        public long GetValue() { ... }
        public void SetValue(long value) { ... }
    }

    public static Counter NewCounter() { ... }
}

Use it from Java

Counter counter = NewCounter();
counter.SetValue(12345);
counter.Inc();
19

Generated Objective-C API

@interface GoMypkgCounter : NSObject { }
@property(strong, readonly) GoSeqRef *ref;
- (int64_t)Value;
- (void)setValue:(int64_t)v;
- (void)Inc;
@end

FOUNDATION_EXPORT GoMypkgCounter* GoMypkgNewCounter();

Use it from Objective-C

GoMypkgCounter* counter = GoMypkgNewCounter();
[counter setValue:12345];
[counter Inc];
20

How to build it?

21

The gomobile bind command

Simplifies the build process. For example, for Android,

(DEMO)

iOS support is a work in progress.

22

Android Studio Integration

Android Studio 1.2+ supports .aar import.

To update the .aar,

23

The Story of Ivy

The Ivy is a command line tool developed by Rob Pike

It's a useful desktop calculator that handles big int, rational and floating-point numbers, vectors, matrices, ...

It is in fact an interpreter for an APL-like language

24

Ivy on Mobile?

~5k lines of Go code (not including tests, docs)

Dependency on math, math/big, math/rand, unicode, ...

Rewriting in Java or Objective-C is a non-starter

25

Ivy apps

Ivy logo by Renée French
26

Gomobile bind

Write it once as a library in Go

Enjoy great language features and packages available in Go

27

Where are we now?

28

Go 1.4: Hello Android!

Released in December 2014

Can build Android apps (arm)

Android builder

The gobind tool for Java and Go language binding

Packages for cross-device apps: basic app control, OpenGL ES 2, touch

29

Go 1.5: Hello iOS!

Planned release early August 2015

Experimental support for iOS (arm,arm64)

iOS builder

30

Go 1.5: Go programs as libraries

Can call Go functions from foreign language in a clean way

31

Go 1.5: Better tools & more packages

golang.org/x/mobile repo getting better

32

Go 1.6+

33

Contributions from Go community

git log | word_cloud
34

Thank you

GopherCon 2015

Hana Kim

Google

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)