安装 gomobile
1.go install 安装
go install golang.org/x/mobile/cmd/gomobile@latest
这个安装之后提示 gomobile not found, 用下面的方式安装
2.git clone 安装
git clone https://github.com/golang/mobile
cd mobile/cmd/gobind
go build .
cd mobile/cmd/gomobile
go build .
找到 go 的安装目录
which is go
输出 /usr/local/go/bin/go
-
将上面
mobile/cmd/gomobile/
目录下的gomobile
可执行文件拷贝到 /usr/local/go/bin/ 下 (和go 为同级目录) -
将上面
mobile/cmd/gobind/
目录下的gobind
可执行文件拷贝到 /usr/local/go/bin/ 下 (和go 为同级目录)
创建项目
mkdir test-hello
cd test-hello
go mod init test-hello
gomobile init
touch hello.go
编辑 hello.go文件, 内容如下:
package hello
import "fmt"
func Hello(name string) {
fmt.Printf("Hello %s!\n", name)
}
打包 xcframework
gomobile bind -target=ios
如果报错
unable to import bind: no Go package in golang.org/x/mobile/bind
执行
go get golang.org/x/mobile/bind
完成之后继续执行 gomobile bind -target=ios
在当前目录下生成 Hello.xcframework 文件
将 Hello.xcframework
拖入 iOS 工程
调用如下:
import Hello
HelloHello("Tyler")