-
Notifications
You must be signed in to change notification settings - Fork 4
/
2015-gophercon-mobile.slide
327 lines (184 loc) · 6.79 KB
/
2015-gophercon-mobile.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
Go on Mobile
GopherCon 2015
Hana Kim
Google
* 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
- Camlistore android app (out-of-process model)
- Goandroid+Mandala (in-process model)
- ...
* golang.org/x/mobile
Goal: Bring Go to Mobile Platforms
Why?
- Use Go to program a complete system (server/client)
- Write a single cross-platform Go library
- Bring a simple language and development tooling to mobile
* Two ways of using Go
Native Apps
- Write the whole app in Go
- Use Go packages for graphics, event handling, audio, etc.
SDK Apps
- Write Android UI in Java, iOS UI in Objective-C/Swift
- Write common functionality in Go as a library
* Native Apps
* Challenge #1: Cross-platform APIs
Work for Android, iOS, and Desktop environments
Provide a rich set of APIs
Follow idiomatic Go style
* Demo: Hello, Gopher!
.image 2015-gophercon-mobile/gophercloud.png 400 _
This program uses the packages from golang.org/x/mobile repo
There is no Java or Objective-C or C in my code
* What's available?
golang.org/x/mobile/...
- [[http://golang.org/x/mobile/app][app: App control]]
- [[http://golang.org/x/mobile/asset][asset: Asset management]]
- [[http://golang.org/x/mobile/gl][gl: OpenGL ES 2]]
- [[https://golang.org/x/mobile/event][event: Events]]
- [[https://golang.org/x/mobile/geom][geom: Screen geometry]]
golang.org/x/mobile/exp/...
- [[https://golang.org/x/mobile/exp/audio][audio: Audio]]
- [[https://golang.org/x/mobile/exp/font][font: System font]]
- [[https://golang.org/x/mobile/exp/sprite][sprite: 2-D rendering]]
- [[https://golang.org/x/mobile/exp/sensor][sensor: Sensors]]
* Challenge #2: Build systems
Dealing with
- Toolchain installation
- Cross compilation for `GOOS/GOARCH` combos
- Android/iOS-specific build details
That is not fun!
* 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)
* SDK Apps
* Go as a library
Go 1.5 can build Go programs as a library that can be used by non-Go programs
- Shared library for dynamic linking (`-buildmode=c-shared`)
- Archive file for static linking (`-buildmode=c-archive`)
Functions marked with `//export` cgo annotations are callable.
.link http://golang.org/s/execmodes
* Working with Foreign Languages
.image 2015-gophercon-mobile/gobind.png 300 _
Manually mapping data structures and functions between languages is tedious and error-prone!
* 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 [[http://golang.org/x/mobile/cmd/gobind][subset of Go types]]
* 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);
* Binding Structs
package mypkg
type Counter struct {
Value int64
}
func (c *Counter) Inc() {
c.Value++
}
func NewCounter() *Counter {
return &Counter{}
}
* 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();
* 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];
* How to build it?
.image 2015-gophercon-mobile/memegobind.jpg 500 _
* The gomobile bind command
Simplifies the build process. For example, for Android,
- Generates language bindings for Go packages
- Compiles Go code to a shared library
- Compiles the generated target language code
- Bundles everything into a `.aar` file (modern way to distribute android libraries)
(DEMO)
iOS support is a work in progress.
* Android Studio Integration
Android Studio 1.2+ supports `.aar` import.
.image 2015-gophercon-mobile/androidstudio2.png 250 _
To update the .aar,
- Build script to invoke `gomobile` `bind`, or
- Gradle plugin to invoke `gomobile` `bind` and publish the output
* The Story of Ivy
The [[robpike.io/ivy][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, ...
.image 2015-gophercon-mobile/ivyscreenshot2.png 300 _
It is in fact an interpreter for an [[https://en.wikipedia.org/wiki/APL_(programming_language)][APL]]-like language
* Ivy on Mobile?
~5k lines of Go code (not including tests, docs)
Dependency on `math`, `math/big`, `math/rand`, `unicode`, ...
.image 2015-gophercon-mobile/canihas.jpg 300 _
Rewriting in Java or Objective-C is a non-starter
* Gomobile bind
.image 2015-gophercon-mobile/ivymobile.png 300 _
.caption Ivy logo by [[http://www.reneefrench.com][Renée French]]
Write it once as a library in Go
Enjoy great language features and packages available in Go
* Where are we now?
* 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
.link https://golang.org/s/go14android
.link https://golang.org/s/gobind
* Go 1.5: Hello iOS!
Planned release early August 2015
Experimental support for iOS (`arm,arm64`)
iOS builder
* Go 1.5: Go programs as libraries
Can call Go functions from foreign language in a clean way
.link golang.org/s/execmodes
* Go 1.5: Better tools & more packages
`golang.org/x/mobile` repo getting better
- The `gomobile` tool for mobile app/library build
- Extended `gobind` tool: Objective-C binding
- `golang.org/x/mobile/exp`: experimenting with audio, sensor, sprite, ...
* Go 1.6+
- Improvement in GL/UI packages
- More APIs available to "pure Go" apps
- Testing, profiling, debugging
- Support for more platforms (e.g. android/x86, iOS simulator)
- Richer type support in `gobind`
- IDE integration
* Contributions from Go community
.image 2015-gophercon-mobile/contributors.png 480 _
.caption git log | word_cloud