blob: 7cc701dac83d368ca72a4ae9715fc5d153670c50 [file] [log] [blame]
Go on Android
A preview
DroidCon
20 Sep 2014
David Crawshaw
Google
@davidcrawshaw
* A brief introduction to Go
* What is Go
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: [[https://talks.golang.org/2012/splash.article]]
* Five years later
Go is not driven by a platform.
Sinks or swims on its own merits.
.image droidcon/gopherswim.jpg
Many users, e.g.
SoundCloud, Docker, Secret, The New York Times
"The cloud programming language."
* Beyond Cloud
Go 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?
* Go on Android
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.
* 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.
* Two ways to use Go
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.
.image droidcon/gopherswrench.jpg
* Go libraries for apps
* Cross language interfaces
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.
[[http://golang.org/s/gobind]]
* gobind basics
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")
* gobind features
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.
* All-Go apps
* NDK-style interfaces
Go will have common libraries:
- Touch events
- OpenGL
- Basic app management
In general: if it works on the NDK and iOS, it works in Go.
* Games
The 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.
* Status: 2014
Android 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`
- OpenGL ES 2 bindings
- touch events package
- limited android build integration
Sprite library will be in early testing.
Setup will still be a little trickier than I want, but it will work.
* Demo
* Roadmap: mid-2015
The 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.
* Questions
* Backup slides
* Go compared to Java
- pointers
- generate less garbage
type 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.?`
- focus on functions, simplicity
- native binaries
More at: [[https://talks.golang.org/2014/go4java.slide]]