无法在Mac上运行gomobile init或version。

huangapple go评论83阅读模式
英文:

Unable to run gomobile init or version on mac

问题

我是一个Go语言的新手。我正在尝试在MacOS Big Sur上安装gomobile进行评估,但无法通过init步骤。

运行go version返回go version go1.17.6 darwin/amd64

按照这些说明gomobile的安装似乎正常工作(在将gomobile显式添加到我的PATH之后),但运行initversion失败。

gomobile: go install golang.org/x/mobile/cmd/gobind failed: exit status 1
go install: version is required when current directory is not in a module
	Try 'go install golang.org/x/mobile/cmd/gobind@latest' to install the latest version
gomobile version unknown: cannot test gomobile binary: exit status 1, no required module provides package golang.org/x/mobile/cmd/gomobile: go.mod file not found in current directory or any parent directory; see 'go help modules'

我猜这与环境变量有关,但对于任何建议和/或帮助,我都会非常感激。

英文:

I am a go newbie. I am trying to install gomobile for evaluation on MacOS BigSur but can't get past the init step.

running go version returns go version go1.17.6 darwin/amd64

Following the directions, the gomobile install appears to work properly (after explicitly adding gomobile to my PATH), but running init or version fails.

gomobile: go install golang.org/x/mobile/cmd/gobind failed: exit status 1
go install: version is required when current directory is not in a module
	Try 'go install golang.org/x/mobile/cmd/gobind@latest' to install the latest version
gomobile version unknown: cannot test gomobile binary: exit status 1, no required module provides package golang.org/x/mobile/cmd/gomobile: go.mod file not found in current directory or any parent directory; see 'go help modules'

I'm guessing this has something to do with environment variables but any suggestions and/or help would be appreciated.

答案1

得分: 5

我确实设法最终设置好了gomobile,但这个过程非常痛苦,官方的“实验性”文档不完整,也没有什么帮助。以下是在MacOS(BigSur)上适用于我的步骤:

按照网站上的步骤安装GO

安装gomobile

go install golang.org/x/mobile/cmd/gomobile@latest

安装Xcode命令行工具。如果已安装但找不到,可能需要运行以下命令:

xcode-select -r

安装Android NDK。可以通过Android Studio的Tools->SDK Manager来完成。需要确保安装的版本受到gomobile支持,因为gomobile不支持最新版本的NDK。

更新shell以包含以下导出项:

export PATH=$PATH:/Users/mikem/go/bin
export ANDROID_HOME=/Users/mikem/Library/Android/sdk
export ANDROID_NDK_HOME=/Users/mikem/Library/Android/sdk/ndk/23.1.7779620

创建一个工作目录

mkdir myworkdir

创建一个模块目录

mkdir mymodule
cd mymodule

在该模块目录中创建一个.go文件,并给它一些内容。注意,包名应与模块名不同,否则绑定可能会抱怨找不到任何内容

package mymodulelib

import "fmt"

func SayHello() {
        fmt.Println("Hello from mymodule")
}

从工作目录中初始化模块

go mod init mymodule

安装gobind - 它会抱怨你应该使用go install方法而不是go get(已弃用),但是go install对我来说不起作用

go get golang.org/x/mobile/cmd/gobind

返回到模块目录,初始化并生成iOS和Android代码

cd mymodule
gomobile init
gomobile bind -target ios
gomobile bind -target android
英文:

I did manage to finally get gomobile set up, but the process was painful and the official "experimental" documentation was incomplete and not at all helpful. Here's the steps that worked for me on MacOS (BigSur):

Follow steps to install GO from website

Install gomobile

go install golang.org/x/mobile/cmd/gomobile@latest

Install Xcode command line tools. If installed but not found, you may need to run the following:

xcode-select -r

Install Android NDK. This can be done via Android Studio’s Tools->SDK Manager. Need to ensure that the installed version is supported by gomobile since the latest version of the NDK is NOT supported by gomobile.

Update shell to include the following exports:

export PATH=$PATH:/Users/mikem/go/bin
export ANDROID_HOME=/Users/mikem/Library/Android/sdk
export ANDROID_NDK_HOME=/Users/mikem/Library/Android/sdk/ndk/23.1.7779620

Create a working directory

mkdir myworkdir

Create a module directory

mkdir mymodule
cd mymodule

Create a .go file in that module directory and give it some content. Note that the package name should be different than the module name or the bind may complain that it can't find anything

package mymodulelib

import "fmt"

func SayHello() {
        fmt.Println("Hello from mymodule")
}

From the work directory, initialize the module

go mod init mymodule

Install gobind - it will complain that you should be using the go install method instead of the go get (deprecated) but the go install does not work (at least for me)

go get golang.org/x/mobile/cmd/gobind

Go back into the module directory, initialize and then generate iOS and android code

cd mymodule
gomobile init
gomobile bind -target ios
gomobile bind -target android

huangapple
  • 本文由 发表于 2022年1月11日 04:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/70658553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定