doc.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package app lets you write portable all-Go apps for Android and iOS.
  6. There are typically two ways to use Go on Android and iOS. The first
  7. is to write a Go library and use `gomobile bind` to generate language
  8. bindings for Java and Objective-C. Building a library does not
  9. require the app package. The `gomobile bind` command produces output
  10. that you can include in an Android Studio or Xcode project. For more
  11. on language bindings, see https://golang.org/x/mobile/cmd/gobind.
  12. The second way is to write an app entirely in Go. The APIs are limited
  13. to those that are portable between both Android and iOS, in particular
  14. OpenGL, audio, and other Android NDK-like APIs. An all-Go app should
  15. use this app package to initialize the app, manage its lifecycle, and
  16. receive events.
  17. # Building apps
  18. Apps written entirely in Go have a main function, and can be built
  19. with `gomobile build`, which directly produces runnable output for
  20. Android and iOS.
  21. The gomobile tool can get installed with go get. For reference, see
  22. https://golang.org/x/mobile/cmd/gomobile.
  23. For detailed instructions and documentation, see
  24. https://golang.org/wiki/Mobile.
  25. # Event processing in Native Apps
  26. The Go runtime is initialized on Android when NativeActivity onCreate is
  27. called, and on iOS when the process starts. In both cases, Go init functions
  28. run before the app lifecycle has started.
  29. An app is expected to call the Main function in main.main. When the function
  30. exits, the app exits. Inside the func passed to Main, call Filter on every
  31. event received, and then switch on its type. Registered filters run when the
  32. event is received, not when it is sent, so that filters run in the same
  33. goroutine as other code that calls OpenGL.
  34. package main
  35. import (
  36. "log"
  37. "golang.org/x/mobile/app"
  38. "golang.org/x/mobile/event/lifecycle"
  39. "golang.org/x/mobile/event/paint"
  40. )
  41. func main() {
  42. app.Main(func(a app.App) {
  43. for e := range a.Events() {
  44. switch e := a.Filter(e).(type) {
  45. case lifecycle.Event:
  46. // ...
  47. case paint.Event:
  48. log.Print("Call OpenGL here.")
  49. a.Publish()
  50. }
  51. }
  52. })
  53. }
  54. An event is represented by the empty interface type interface{}. Any value can
  55. be an event. Commonly used types include Event types defined by the following
  56. packages:
  57. - golang.org/x/mobile/event/lifecycle
  58. - golang.org/x/mobile/event/mouse
  59. - golang.org/x/mobile/event/paint
  60. - golang.org/x/mobile/event/size
  61. - golang.org/x/mobile/event/touch
  62. For example, touch.Event is the type that represents touch events. Other
  63. packages may define their own events, and send them on an app's event channel.
  64. Other packages can also register event filters, e.g. to manage resources in
  65. response to lifecycle events. Such packages should call:
  66. app.RegisterFilter(etc)
  67. in an init function inside that package.
  68. */
  69. package app