Go on Android
A preview
DroidCon
20 Sep 2014
David Crawshaw
DroidCon
20 Sep 2014
David Crawshaw
Go is a general purpose programming language.
Born out of frustration with C++ and Java:
- Slow builds
- Too much complexity
Go is fast and simple.
More at: /talks/2012/splash.article
3
Go is not driven by a platform.
Sinks or swims on its own merits.
Many users, e.g.
SoundCloud, Docker, Secret, The New York Times
"The cloud programming language."
4Go has found other uses.
Popular on embedded linux systems.
PayPal's Beacon hardware is powered by Go.
Large-scale data analysis.
For many of us, it replaces python/perl/ruby.
Where else can we use Go?
What about phones and tablets?
Android UI programming needs lots of Java APIs.
My first experiment was using these from Go.
It did not work.
Using Java APIs in Go is writing Java using Go syntax.
"You can write FORTRAN in any language"
So does it ever make sense to use Go on Android?
Yes, for portability.
Lots of apps are written for more than just Android.
Some apps start elsewhere and never make it to Android.
:-(
Today, developers solve platform portability with C++.
We can do better.
1. Write libraries in Go, use them from Java apps. Or Objective-C/Swift apps.
2. Write apps entirely in Go, restricted to a set of common APIs across platforms.
Java is a silo.
To use another language, you need JNI.
JNI is tricky, buggy, painful.
It keeps Java programmers away from many good things.
So, no JNI.
Instead, we have a tool for that: gobind
It generates Java interfaces for you.
package hi import "fmt" func Hello(name string) { fmt.Println("Hello, %s!\n", name) }
Use gobind
on package hi
to generate Go helper code and a Java interface:
package go; public abstract class Hi { public static final void Hello(String name) { .. } }
Invoke from Java:
Hi.Hello("DroidCon")
Today, gobind supports many basic Go types, structs, and callbacks.
When finished, gobind
will support all Go types.
Go's simplicity makes language bindings simple.
For C++, SWIG has many hard-to-use features.
With Go we get configuration-free language bindings.
SWIG without the .swig files.
Go will have common libraries:
In general: if it works on the NDK and iOS, it works in Go.
14The primary target for pure Go apps is games.
Better control over over allocation means fewer
garbage collector problems.
Unlikely language for high-budget 3D engines.
But lots of games can be written in Go.
But we are building a 2D sprite package.
15Android OS support will be built into the Go runtime in the December 1.4 release.
First version available from the go.mobile
subrepository of
gobind
Sprite library will be in early testing.
Setup will still be a little trickier than I want, but it will work.
16The plan is iOS support will be in the Go runtime July 1.5 release.
The same OpenGL bindings and touch events package will work. The gobind
tool will generate Objective-C/Swift bindings.
Sprite will be ready for 2D games.
18type Point struct { X float64 Y float64 } type Points []Point
map[int]int
is efficient like android.os.SparseIntArray
map[int]float32
is efficient like android.os.?
More at: /talks/2014/go4java.slide
21DroidCon
20 Sep 2014