Go on Android

GothamGo

15 Nov 2014

David Crawshaw

Google

Video

This talk was presented at GothamGo in New York City, November 2014.

Go on Mobile

The goal is to bring Go to Android and iOS,
starting with Android.

This is new territory for Go and a well-established
ecosystem. A lot of experimentation is necessary.

Today

Go 1.4 Status

The coming 1.4 release can build binaries for Android OS.

With the mobile subrepository and the Android SDK/NDK, it can:

What's there

Packages for cross-device apps:

What's missing — a good build system

There's more than one way to build an Android App.

None of them

We are working on this for Go 1.5.

Until it is done, using Go on Android requires some bravery.

What it's for — the two kinds of apps

SDK Apps

Write your Android UI in Java.
Write your iOS UI in Objective-C/Swift.
Write your logic in Go.

Share the Go using interfaces generated by gobind.

NDK Apps

Games.

Use OpenGL or the coming 2D sprite package to write to the screen.
Everything is written in Go.

Hello, World!

Portable APIs, just Go. This is a complete app:

package main

import (
    "golang.org/x/mobile/app"
    "golang.org/x/mobile/app/debug"
    "golang.org/x/mobile/gl"
)

func main() {
    app.Run(app.Callbacks{
        Draw: draw,
    })
}

func draw() {
    gl.ClearColor(1, 0, 0, 1) // RGBA value used to clear buffer: red
    gl.Clear(gl.COLOR_BUFFER_BIT)
    debug.DrawFPS()
}

Write anywhere

Developing with portable APIs means starting from very little.
It is a lot of work to get going and limits access to device features.

But it also frees us from device-specific build systems.

The app package includes Mac/X11 shims for starting as normal programs.
We can write Apps anywhere we can write Go.

(Windows coming soon.)

Touch events

package main

import (
    "fmt"

    "golang.org/x/mobile/app"
    "golang.org/x/mobile/event"
    "golang.org/x/mobile/gl"
)

func main() {
    app.Run(app.Callbacks{
        Draw: func() {
            gl.ClearColor(0, 0, 1, 1) // blue
            gl.Clear(gl.COLOR_BUFFER_BIT)
        },
        Touch: func(e event.Touch) { fmt.Println(e) },
    })
}

Package sprite

We are building a 2D rendering and compositing package.

It renders a scene graph. A scene starts with a root
*sprite.Node, which can have child nodes.

Each node may have an affine transform and texture.

Rendering is done in a depth-first traversal of the scene,
with transforms applied relative to the parent.

Composition is done in OpenGL.

Sprite — affine transform

Sprite — depth first traversal, applying parent's transforms

Sprite Demo

Sprite — what's next

Go 1.5 Plans (July 2015)

Thank you

GothamGo

15 Nov 2014

David Crawshaw

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.)