forked from eliasnaur/golab-2017-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
golab-2017-training-nocomments.slide
149 lines (94 loc) · 2.48 KB
/
golab-2017-training-nocomments.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
Write your next Android app in Go
GoLab, Florence 2017
Elias Naur
@elias_naur
* Prerequisites
- Laptop running a 64 bit OS; Linux, macOS or Windows.
- Go 1.7 (64 bit).
- Android SDK/Android studio.
- Android NDK (64 bit), downloaded through the Android SDK manager. Command line:
sdkmanager ndk-bundle
- A working and recent Android device or emulator.
- On a device, enable developer mode and USB debugging.
* Setup
- Environment
- ANDROID_HOME should point to your Android SDK
- gomobile
$ go get -u golang.org/x/mobile/cmd/...
$ gomobile init
* Training files
Slides
https://talks.godoc.org/github.com/eliasnaur/golab-2017-training/golab-2017-training-nocomments.slide
Example code
$ git clone https://github.com/eliasnaur/golab-2017-training.git
Add the golab-2017-training directory to your GOPATH.
* The first run
$ cd golab-2017-basic/basic
Linux/macOS
$ ./gradlew installDebug
Windows
$ gradlew.bat installDebug
* Logging
$ abd log cat -s Go:* GoLog:*
Java
android.util.Log.i("Go", ...);
* Structs
Go
type AGoStruct struct {
...
}
func NewAGoStruct() *AGoStruct {
...
}
func (s *AGoStruct) AGoMethod() {
...
}
Java
goapp.AGoStruct s = new goapp.AGoStruct();
s.aGoMethod()
* Structs (cont.)
You can use all the basic types (string, int, float32 etc.), other structs and interfaces.
Go
func F(str *GoStruct, interf GoInterface, i int, f float32) string
* Go interfaces
Go
type SomeGoInterface interface {
F(i int) string
}
Java
class ImplementsGo implements goapp.SomeGoInterface {
@Override public String f(i long, s goapp.AGoStruct) {
...
}
}
* Go interfaces (cont.)
Go
func TakesAnInterface(i SomeGoInterface) string {
return i.F(10)
}
Java
String res = goapp.takesAnInterface(new ImplementsGo());
android.util.Log.i("Go", "The result: " + res);
* Reverse bindings
Go
package gorev
import (
"Java/android/os"
"Java/android/support/v7/app"
"Java/android/databinding/DataBindingUtil"
"Java/gorev/databinding/ActivityMainBinding"
rlayout "Java/gorev/R/layout"
// The Java package for "gorev".
gopkg "Java/gorev"
)
type GoActivity struct {
app.AppCompatActivity
}
* Reverse bindings (cont.)
func (a *GoActivity) OnCreate(this gopkg.GoActivity, b os.Bundle) {
this.Super().OnCreate(b)
db := DataBindingUtil.SetContentView(this, rlayout.Activity_main)
binding := ActivityMainBinding.Cast(db)
binding.SetText("Hello from the reverse")
}