Supported Go types for gomobile bind
gomobile bind
With gomobile we can generate language bindings that make it possible to call Go functions from Java. And it’s awesome. Now you can write Android applications in Go (unfortunately without UI features and with pure SDK coverage, but I hope it will grow up from experiment to production-ready tool).
Requirements
- golang 1.5+
- go get golang.org/x/mobile/cmd/gomobile
- gomobile init
- Install Android SDK to ~/android-sdk-linux
- Install java-jdk
Playing with Go code
Let’s create some artificial code and try to import it into Java. The following package just returns random user and full Information.
gomobilebindtest.go:
|
|
It’s a valid Go code, but let’s try to run a gomobile bind with it:
|
|
Ugh… it gives me a lot of errors, because not all Go types are supported in bindings. Let’s comment these restricted types and run bind again:
|
|
Success! Here is a list of currently supported types (got it from golang.org/x/mobile/cmd/gobind):
- Signed integer and floating point types.
- String and boolean types.
- Byte slice types. Note the current implementation does not support data mutation of slices passed in as function arguments.
- Any function type all of whose parameters and results have supported types. Functions must return either no results, one result, or two results where the type of the second is the built-in ’error’ type.
- Any interface type, all of whose exported methods have supported function types.
- Any struct type, all of whose exported methods have supported function types and all of whose exported fields have supported types.
It gives us some limitations to use Go in Android, to use Go packages. But anyway it’s a nice start. Later I will make an article with real working Android app with Go packages.