{"id":22309137,"url":"https://github.com/csnewman/flutter-go-bridge","last_synced_at":"2025-09-02T15:30:44.879Z","repository":{"id":186967265,"uuid":"676076311","full_name":"csnewman/flutter-go-bridge","owner":"csnewman","description":"Flutter/Dart to Go FFI generator ","archived":false,"fork":false,"pushed_at":"2024-05-30T19:24:49.000Z","size":453,"stargazers_count":59,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-17T15:31:18.853Z","etag":null,"topics":["dart","ffi","flutter","go","golang"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/csnewman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-08-08T11:32:24.000Z","updated_at":"2024-12-07T07:05:11.000Z","dependencies_parsed_at":"2024-04-13T12:24:30.002Z","dependency_job_id":"05aa4cfe-c384-4ffa-8fa5-0e889d3aadb4","html_url":"https://github.com/csnewman/flutter-go-bridge","commit_stats":null,"previous_names":["csnewman/flutter-go-bridge"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csnewman%2Fflutter-go-bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csnewman%2Fflutter-go-bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csnewman%2Fflutter-go-bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csnewman%2Fflutter-go-bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/csnewman","download_url":"https://codeload.github.com/csnewman/flutter-go-bridge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231794009,"owners_count":18427531,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","ffi","flutter","go","golang"],"created_at":"2024-12-03T20:16:59.159Z","updated_at":"2024-12-29T22:45:28.253Z","avatar_url":"https://github.com/csnewman.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# flutter-go-bridge\n\nReuse existing Go libraries in Flutter by calling Go from Dart using auto generated bindings.\n\n### Features\n\n- MacOS, iOS, Windows, Linux and Android support.\n- Sync and Async calls from Dart.\n- Passing primitives (various `int`'s), `string`'s and nested structures.\n- Automatic `error` to `Exception` mapping.\n- Basic object passing.\n\n### Getting Started\n\n1. Install fgb\n   ```\n   go get -u github.com/csnewman/flutter-go-bridge\n   ```\n\n2. Create a wrapper around your Go code.\n    - See [Go wrapper](#go-wrapper) below.\n    - See [example](https://github.com/csnewman/flutter-go-bridge/blob/master/exampleapp/go/example.go) for an example.\n\n3. Generate bindings\n    - Run `go generate` in the directory containing your wrapper.\n\n4. Use the generated bindings from your Flutter/Dart application\n\n5. Automate library building by integrating into flutter build.\n    - See [Platform building](#platform-building) below.\n    - See the `exampleapp` folder for a full example.\n\n6. When modifying the Go code, you may need to call `flutter clean` to trigger a rebuild, dependent upon your Go source \n   location and configured source directories.\n\n### Go wrapper\n\nThe bridge does not support all Go constructs and instead relies on a subset. This allows for a more ergonomic wrapper.\n\nTherefore, you should create a Go package containing a wrapper around more complex Go libraries. The generator will scan\nthis package and generate a `bridge` package containing the necessary FFI bridging logic.\n\nSee [example](https://github.com/csnewman/flutter-go-bridge/blob/master/exampleapp/go/example.go) for a complete example.\n\nThe process is as follows:\n\n1. `mkdir go` (inside the Flutter project)\n2. Create `go/example.go`, containing the following:\n   ```go\n    //go:generate go run github.com/csnewman/flutter-go-bridge/cmd/flutter-go-bridge generate --src example.go --go bridge/bridge.gen.go --dart ../lib/bridge.gen.dart\n    package example\n   ```\n   Replace the `--dart` path, with a suitable location inside your flutter applications `lib` directory.\n3. Write a simplified wrapper, using the [template mappings](#mappings) below.\n4. Invoke `go generate` to automatically regenerate the `bridge.gen.go` and the corresponding `bridge.gen.dart` file.\n\n### Dart setup\n\nFrom Dart:\n\n1. Import generated bridge\n   ```dart\n   import 'bridge.gen.dart';\n   ```\n2. Load library\n   ```dart\n   var bridge = Bridge.open();\n   ```\n3. Invoke functions\n   ```dart\n   bridge.example();\n   await bridge.exampleAsync();\n   ```\n\n### Mappings\n\nThe following patterns are supported:\n\n#### Function calls\n\n```go\nfunc Simple(a int, b int) int {\n    return a + b\n}\n```\n\nWill produce:\n\n```dart\nint simple(int a, int b);\n\nFuture\u003cint\u003e simpleAsync(int a, int b);\n```\n\n#### Function calls, with errors\n\n```go\nfunc SimpleError(a int, b int) (int, error) {\n    return 0, errors.New(\"an example\")\n}\n```\n\nWill produce:\n\n```dart\nint simpleError(int a, int b);\n\nFuture\u003cint\u003e simpleErrorAsync(int a, int b);\n```\n\nIf the Go function returns an error, the `simpleError` function will throw a `BridgeException`.\n\n#### Struct passing\n\n```go\ntype ExampleStruct struct {\n    A int\n    B string\n}\n\nfunc StructPassing(val ExampleStruct) {\n}\n```\n\nWill produce:\n\n```dart\nfinal class ExampleStruct {\n    int a;\n    String b;\n\n    ExampleStruct(this.a, this.b);\n}\n```\nand\n```dart\nvoid structPassing(ExampleStruct val);\n\nFuture\u003cvoid\u003e structPassingAsync(ExampleStruct val);\n```\n\nStructs passed in this manner will be passed by `value`, meaning the contents will be copied.\n\nValue structs can not contain private fields.\n\n#### Objects\n\nTODO: Document\n\n### Platform building\n\nThe platforms supported by `flutter` use various build tooling, which complicates integrating Go into the build\npipeline. Originally this project had hooks into the build systems for Windows, Linux and Android, however this had\nhigh maintenance and was not trivial to integrate into the Mac ecosystem.\n\nFlutter (\u0026 Dart) currently have an experimental feature called\n[Native Assets](https://github.com/flutter/flutter/issues/129757) which greatly simplifies the setup. This does however\nmean that for now, this project requires using the flutter `master` channel.\n\n#### Native Assets approach\n\nA complete example can be seen in the `exampleapp` folder.\n\n1. Switch to the `master` flutter channel\n   ```bash\n   flutter channel master\n   ```\n2. Enable the [Native Assets](https://github.com/flutter/flutter/issues/129757) experiment\n   ```bash\n   flutter config --enable-native-assets\n   ```\n3. Add the required dependencies to `pubspec.yaml`\n   ```yaml\n   logging: ^1.2.0\n   native_assets_cli: ^0.5.4\n   native_toolchain_go: ^0.3.0\n   ffi: ^2.1.2\n   ```\n4. Fetch dependencies\n   ```bash\n   flutter pub get\n   ```\n5. Create a `build.dart` file\n   ```dart\n   import 'package:native_toolchain_go/go_native_toolchain.dart';\n   import 'package:logging/logging.dart';\n   import 'package:native_assets_cli/native_assets_cli.dart';\n   \n   void main(List\u003cString\u003e args) async {\n     await build(args, (config, output) async {\n       final packageName = config.packageName;\n\n       final goBuilder = GoBuilder(\n         name: packageName,\n         assetName: 'bridge.gen.dart',\n         bridgePath: 'go/bridge',\n         sources: ['go/'],\n         dartBuildFiles: ['hook/build.dart'],\n       );\n      \n       await goBuilder.run(\n         buildConfig: config,\n         buildOutput: output,\n         logger: Logger('')\n           ..level = Level.ALL\n           ..onRecord.listen((record) =\u003e print(record.message)),\n       );\n     });\n   }\n   ```\n   The `assetId` path needs to match the location of the autogenerated `bridge.gen.dart` file, as flutter uses this\n   internally to automate library resolution. You may need to specify a list of source directories to the `GoBuilder`\n   to allow automatic rebuilding as necessary.\n\nYou should now be able to use your IDE and other tooling as usual.\n\n#### Manual building\n\nIf you do not want to use the `master` channel or wish to customise the build process, you can manually build the Go\nlibrary and bundle with your application as necessary:\n\n```bash\nCGO_ENABLED=1 go build -buildmode=c-shared -o libexample.so example/bridge/bridge.gen.go\n```\n\nYou can specify `GOOS` and `GOARCH` as necessary.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsnewman%2Fflutter-go-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcsnewman%2Fflutter-go-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsnewman%2Fflutter-go-bridge/lists"}