安装 Go
参考官方文档,安装并配置相关环境
https://go.dev/
安装 gomobile
gomobile 需要go 1.6及以上
go install golang.org/x/mobile/cmd/gomobile@latest
# 或
go get golang.org/x/mobile/cmd/gomobile@latest
配置环境变量
GOPATH=$HOME/go
GOPATH_BIN=$HOME/go/bin
PATH=$PATH:$GOPATH
PATH=$PATH:$GOROOT
PATH=$PATH:$GOPATH_BIN
创建Go模块
mkdir hqkit
cd hqkit
go mod init hqkit
初始化 gomobile
gomobile init
# 安装bind依赖
go get golang.org/x/mobile/bind
编写代码后,打包库
rm -rf HqKit.xcframework
gomobile bind -target=ios -o HqKit.xcframework ./hqkit
# gomobile -v bind -target=<ios,android ...> -o <xxx.xcframework or xxx.aar or xxx.jar> <<go_pacakge_name_path>
# -o ios为 xxx.xcframework, android 为 xxx.aar或xxx.jar
Go支持导出的类型
https://pkg.go.dev/golang.org/x/mobile/cmd/gobind#hdr-Type_restrictions
Signed integer and floating point types.
String and boolean types.
Byte slice types. Note that byte slices are passed by reference, and support mutation.
Any function type all of whose parameters and results have supported types. Functions must return either no results, one result, or two results where the type of the second is the built-in 'error' type.
Any interface type, all of whose exported methods have supported function types.
Any struct type, all of whose exported methods have supported function types and all of whose exported fields have supported types.
支持类型中文说明
基本类型:
有符号整数和浮点数
字符串和bool
结构体:
结构体成员仅支持的基本类型才能导出
切片:
支持的基本类型才能导出
函数:
可以无返回值,有返回值时,最多支持2个返回值,第二个必须是 error 类型
函数的参数和返回值必须值支持的基本类型才能导出