| 1 | # Share Go logic with a native companion |
| 2 | |
| 3 | Use Go Mobile when the app already has useful Go logic to share. Keep the |
| 4 | binding surface in a small package independent of HTTP handlers, the server |
| 5 | bootstrap and UI. The app's normal Go module owns its engine and tool pins; |
| 6 | Rastrillo Native does not make Go a dependency of Swift-only consumers. |
| 7 | |
| 8 | Install `gomobile` and `gobind` from the same reviewed `golang.org/x/mobile` |
| 9 | revision, record that revision in the app's build configuration, and run |
| 10 | `gomobile init`. Keep that module in the engine's dependency graph as well. |
| 11 | Do not silently update a binding tool during a release build. |
| 12 | |
| 13 | From the engine module, with its exported binding package at `./mobile`: |
| 14 | |
| 15 | ```sh |
| 16 | gomobile bind -target=ios,iossimulator,macos -o AppEngine.xcframework ./mobile |
| 17 | gomobile bind -target=android -o appengine.aar ./mobile |
| 18 | ``` |
| 19 | |
| 20 | Apple builds need macOS and Xcode. Android builds need the Java toolchain, |
| 21 | Android SDK and NDK. Use `gomobile help bind` from the pinned toolchain for |
| 22 | its supported targets and flags. Import the XCFramework in the Apple app |
| 23 | and the AAR in the Android app; UI and lifecycle remain platform code. |
| 24 | |
| 25 | These are integration recipes, not outputs validated by this package's CI. |
| 26 | Before adopting a bridge, compile it for each supported target and exercise |
| 27 | it through Swift/Kotlin, including errors, callbacks and cancellation. |
| 28 | |
| 29 | Only a subset of Go types can cross bindings. Prefer a small API carrying |
| 30 | strings, numbers and byte slices; keep complex internal types behind it. |
| 31 | Specify who owns callback lifetimes and which thread receives them, avoid |
| 32 | blocking the UI thread, and batch work where repeated crossings are costly. |
| 33 | Browser clients can keep a WebCrypto or JavaScript implementation pinned by |
| 34 | the same test vectors. Neither a Swift package nor a mobile binding makes |
| 35 | the web client use the Go engine automatically. |
| 36 | |
| 37 | Source: [Go Mobile command documentation](https://pkg.go.dev/golang.org/x/mobile/cmd/gomobile) |
| 38 | and [Go Mobile binding model](https://go.dev/wiki/Mobile). |
| 39 | |