安裝 Go
參考官方文檔,安裝并配置相關(guān)環(huán)境
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
配置環(huán)境變量
GOPATH=$HOME/go
GOPATH_BIN=$HOME/go/bin
PATH=$PATH:$GOPATH
PATH=$PATH:$GOROOT
PATH=$PATH:$GOPATH_BIN
創(chuàng)建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支持導(dǎo)出的類型
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.
支持類型中文說明
基本類型:
有符號整數(shù)和浮點數(shù)
字符串和bool
結(jié)構(gòu)體:
結(jié)構(gòu)體成員僅支持的基本類型才能導(dǎo)出
切片:
支持的基本類型才能導(dǎo)出
函數(shù):
可以無返回值,有返回值時,最多支持2個返回值,第二個必須是 error 類型
函數(shù)的參數(shù)和返回值必須值支持的基本類型才能導(dǎo)出