Text file talks/2015/gophercon-go-on-mobile.slide

     1  Go on Mobile
     2  
     3  GopherCon 2015
     4  
     5  Hana Kim
     6  Google
     7  
     8  
     9  * Video
    10  
    11  A video of this talk was recorded at GopherCon in Denver.
    12  
    13  .link https://www.youtube.com/watch?v=sQ6-HyPxHKg Watch the talk on YouTube
    14  
    15  
    16  * Caution
    17  
    18  .image gophercon-go-on-mobile/caution.png 300 _
    19  
    20  The Go Mobile project is experimental. Use this at your own risk.
    21  
    22  While we are working hard to improve it, neither Google nor the Go
    23  team can provide end-user support.
    24  
    25  * Background
    26  
    27  Mobile support was frequently requested
    28  
    29  Some users built their own Go binaries for Android with cgo + external linking through NDK tool chains
    30  
    31  Some Android Apps used Go even before Go 1.4
    32  
    33  - Camlistore android app (out-of-process model)
    34  - Goandroid+Mandala (in-process model)
    35  - ...
    36  
    37  * golang.org/x/mobile
    38  
    39  Goal: Bring Go to Mobile Platforms
    40  
    41  Why?
    42  
    43  - Use Go to program a complete system (server/client)
    44  - Write a single cross-platform Go library
    45  - Bring a simple language and development tooling to mobile
    46  
    47  * Two ways of using Go
    48  
    49  Native Apps
    50  
    51  - Write the whole app in Go
    52  - Use Go packages for graphics, event handling, audio, etc.
    53  
    54  SDK Apps
    55  
    56  - Write Android UI in Java, iOS UI in Objective-C/Swift
    57  - Write common functionality in Go as a library
    58  
    59  * Native Apps
    60  
    61  * Challenge #1: Cross-platform APIs
    62  
    63  Work for Android, iOS, and Desktop environments
    64  
    65  Provide a rich set of APIs
    66  
    67  Follow idiomatic Go style
    68  
    69  * Demo: Hello, Gopher!
    70  
    71  .image gophercon-go-on-mobile/gophercloud.png 400 _
    72  
    73  This program uses the packages from golang.org/x/mobile repo
    74  There is no Java or Objective-C or C in my code
    75  
    76  * What's available?
    77  
    78  golang.org/x/mobile/...
    79  
    80  - [[https://pkg.go.dev/golang.org/x/mobile/app][app: App control]]
    81  - [[https://pkg.go.dev/golang.org/x/mobile/asset][asset: Asset management]]
    82  - [[https://pkg.go.dev/golang.org/x/mobile/gl][gl: OpenGL ES 2]]
    83  - [[https://pkg.go.dev/golang.org/x/mobile/event][event: Events]]
    84  - [[https://pkg.go.dev/golang.org/x/mobile/geom][geom: Screen geometry]]
    85  
    86  golang.org/x/mobile/exp/...
    87  
    88  - [[https://pkg.go.dev/golang.org/x/mobile/exp/audio][audio: Audio]]
    89  - [[https://pkg.go.dev/golang.org/x/mobile/exp/font][font: System font]]
    90  - [[https://pkg.go.dev/golang.org/x/mobile/exp/sprite][sprite: 2-D rendering]]
    91  - [[https://pkg.go.dev/golang.org/x/mobile/exp/sensor][sensor: Sensors]]
    92  
    93  * Challenge #2: Build systems
    94  
    95  Dealing with
    96  
    97  - Toolchain installation
    98  
    99  - Cross compilation for `GOOS/GOARCH` combos
   100  
   101  - Android/iOS-specific build details
   102  
   103  That is not fun!
   104  
   105  * The gomobile tool
   106  
   107    $ go get golang.org/x/mobile/cmd/gomobile
   108  
   109  Simplifies toolchain installation and app deployment
   110  
   111  To install the Android/iOS compiler tool chain:
   112  
   113  	$ gomobile init
   114  
   115  To build an Android APK and an iOS app
   116  
   117  	$ gomobile -target=android build
   118  	$ gomobile -target=ios     build
   119  
   120  (Demo)
   121  
   122  * SDK Apps
   123  
   124  * Go as a library
   125  
   126  Go 1.5 can build Go programs as a library that can be used by non-Go programs
   127  
   128  - Shared library for dynamic linking (`-buildmode=c-shared`)
   129  - Archive file for static linking (`-buildmode=c-archive`)
   130  
   131  Functions marked with `//export` cgo annotations are callable.
   132  
   133  .link /s/execmodes go.dev/s/execmodes
   134  
   135  * Working with Foreign Languages
   136  
   137  .image gophercon-go-on-mobile/gobind.png 300 _
   138  
   139  Manually mapping data structures and functions between languages is tedious and error-prone!
   140  
   141  
   142  * The gobind tool
   143  
   144  	$ go get golang.org/x/mobile/cmd/gobind
   145  
   146  Automates language binding through code generation
   147  
   148  Defines the language binding from exported Go APIs; no explicit annotation
   149  
   150  Currently supports a [[https://pkg.go.dev/golang.org/x/mobile/cmd/gobind][subset of Go types]]
   151  
   152  
   153  * Binding Functions, Basic Types & Errors
   154  
   155  Go API
   156  
   157  	package mypkg
   158  
   159  	func Hello() (string, error) { return "Gopher", nil }
   160  
   161  Generated Java API
   162  
   163  	public abstract class Mypkg {
   164  	    public static String Hello() throws Exception { ... }
   165  	}
   166  
   167  Generated Objective-C API
   168  
   169  	FOUNDATION_EXPORT BOOL GoMypkgHello(NSString** ret0_, NSError** error);
   170  
   171  * Binding Structs
   172  
   173  	package mypkg
   174  
   175  	type Counter struct {
   176  		Value int64
   177  	}
   178  
   179  	func (c *Counter) Inc() {
   180  		c.Value++
   181  	}
   182  
   183  	func NewCounter() *Counter {
   184  		return &Counter{}
   185  	}
   186  
   187  * Generated Java API
   188  
   189  	public abstract class Mypkg {
   190  		public static final class Counter {
   191  			public void Inc() { ... }
   192  			public long GetValue() { ... }
   193  			public void SetValue(long value) { ... }
   194  		}
   195  
   196  		public static Counter NewCounter() { ... }
   197  	}
   198  
   199  Use it from Java
   200  
   201  	Counter counter = NewCounter();
   202  	counter.SetValue(12345);
   203  	counter.Inc();
   204  
   205  
   206  * Generated Objective-C API
   207  
   208  	@interface GoMypkgCounter : NSObject { }
   209  	@property(strong, readonly) GoSeqRef *ref;
   210  	- (int64_t)Value;
   211  	- (void)setValue:(int64_t)v;
   212  	- (void)Inc;
   213  	@end
   214  
   215  	FOUNDATION_EXPORT GoMypkgCounter* GoMypkgNewCounter();
   216  
   217  Use it from Objective-C
   218  
   219  	GoMypkgCounter* counter = GoMypkgNewCounter();
   220  	[counter setValue:12345];
   221  	[counter Inc];
   222  
   223  * How to build it?
   224  
   225  .image gophercon-go-on-mobile/memegobind.jpg 500 _
   226  
   227  
   228  * The gomobile bind command
   229  
   230  Simplifies the build process. For example, for Android,
   231  
   232  - Generates language bindings for Go packages
   233  
   234  - Compiles Go code to a shared library
   235  
   236  - Compiles the generated target language code
   237  
   238  - Bundles everything into a `.aar` file (modern way to distribute android libraries)
   239  
   240  (DEMO)
   241  
   242  iOS support is a work in progress.
   243  
   244  * Android Studio Integration
   245  
   246  Android Studio 1.2+ supports `.aar` import.
   247  
   248  .image gophercon-go-on-mobile/androidstudio2.png 250 _
   249  
   250  To update the .aar,
   251  
   252  - Build script to invoke `gomobile` `bind`, or
   253  - Gradle plugin to invoke `gomobile` `bind` and publish the output
   254  
   255  
   256  * The Story of Ivy
   257  
   258  The [[https://robpike.io/ivy][Ivy]] is a command line tool developed by Rob Pike
   259  
   260  It's a useful desktop calculator that handles big int, rational and floating-point numbers, vectors, matrices, ...
   261  
   262  .image gophercon-go-on-mobile/ivyscreenshot2.png 300 _
   263  
   264  It is in fact an interpreter for an [[https://en.wikipedia.org/wiki/APL_(programming_language)][APL]]-like language
   265  
   266  * Ivy on Mobile?
   267  
   268  ~5k lines of Go code (not including tests, docs)
   269  
   270  Dependency on `math`, `math/big`, `math/rand`, `unicode`, ...
   271  
   272  .image gophercon-go-on-mobile/canihas.jpg 300 _
   273  
   274  Rewriting in Java or Objective-C is a non-starter
   275  
   276  * Ivy apps
   277  
   278  .image gophercon-go-on-mobile/ivymobile.png 300 _
   279  .caption Ivy logo by [[https://www.reneefrench.com][Renée French]]
   280  .link https://play.google.com/store/apps/details?id=org.golang.ivy Google Play Store
   281  .link https://itunes.apple.com/us/app/ivy-big-number-calculator/id1012116478 Apple App Store
   282  
   283  * Gomobile bind
   284  
   285  Write it once as a library in Go
   286  
   287  Enjoy great language features and packages available in Go
   288  
   289  * Where are we now?
   290  
   291  * Go 1.4: Hello Android!
   292  
   293  Released in December 2014
   294  
   295  Can build Android apps (`arm`)
   296  
   297  Android builder
   298   
   299  The `gobind` tool for Java and Go language binding
   300  
   301  Packages for cross-device apps: basic app control, OpenGL ES 2, touch
   302  
   303  .link /s/go14android go.dev/s/go14android
   304  .link /s/gobind go.dev/s/gobind
   305  
   306  * Go 1.5: Hello iOS!
   307  
   308  Planned release early August 2015
   309  
   310  Experimental support for iOS (`arm,arm64`)
   311  
   312  iOS builder
   313  
   314  * Go 1.5: Go programs as libraries
   315  
   316  Can call Go functions from foreign language in a clean way
   317  
   318  .link /s/execmodes go.dev/s/execmodes
   319  
   320  * Go 1.5: Better tools & more packages
   321  
   322  `golang.org/x/mobile` repo getting better
   323  
   324  - The `gomobile` tool for mobile app/library build
   325  
   326  - Extended `gobind` tool: Objective-C binding
   327  
   328  - `golang.org/x/mobile/exp`: experimenting with audio, sensor, sprite, ...
   329  
   330  * Go 1.6+
   331  
   332  - Improvement in GL/UI packages
   333  
   334  - More APIs available to "pure Go" apps
   335  
   336  - Testing, profiling, debugging
   337  
   338  - Support for more platforms (e.g. android/x86, iOS simulator)
   339  
   340  - Richer type support in `gobind`
   341  
   342  - IDE integration
   343  
   344  * Contributions from Go community
   345  
   346  .image gophercon-go-on-mobile/contributors.png 480 _
   347  .caption git log | word_cloud
   348  
   349  

View as plain text