gomobile 的使用教程

安裝 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)出

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容