在 Ubuntu 16.04 中获取 GOPATH 错误"go: cannot use path@version syntax in GOPATH mode"



我无法在我的$GOPATH文件夹中运行go get git@github<user/repo>。收到此错误:

go:不能在 GOPATH 模式下使用path@version语法

我只是想了解为什么go get即使在安装过程中配置了$GOPATH也无法正常工作。环境是 ubuntu。

~/$ echo $GOPATH
/home/user/go

我遇到了同样的问题,并解决了在我的 .zshrc(或 .bashrc 取决于您使用的 shell)中设置特定的 env 变量export GO111MODULE=on并重新启动 shell 以启用模块。您可以在此处找到更多详细信息:https://github.com/golang/go/wiki/Modules

正如您已经注意到的,您应该使用 go get github.com/<user>/<repo> .

您看到的错误消息来自 go get 年实现的用于支持 Go 模块的新功能 - 您现在还可以指定依赖项的版本:go get github.com/<user>/<repo>@<version> ,其中 version 是使用 semver 的 git 标签,例如 v1.0.2 .

我也遇到了这个问题。经过一些搜索,以下内容使用 go mod 而不是 go get 来工作,这是 Golang 模块的一个功能:

$ export GO111MODULE=on
$ go mod init <project name>
# go mod init HelloWorld
# or
# go mod init .
$ go mod download repo@version
# go mod download github.com/robfig/cron/v3@v3.0.0
在我

用模块初始化项目之前,在一个空项目上运行 $ go get github.com/<user>/<repo>@<version> 时,我在 Go v1.14 中遇到了此错误。

为了解决这个问题,我使用以下方法创建了一个go.mod文件:

$ go mod init

我能够成功重新运行 get 命令,该命令下载了供应商的软件包,更新了go.mod文件,并创建了一个go.sum文件。

按照

https://gist.github.com/nikhita/432436d570b89cab172dcf2894465753 中的说明更新 go 的版本

这对我有用!

如果您在尝试使用模块时收到此错误,则应在获取之前将 dir 更改为项目:

root@host:/# go get github.com/ibm-messaging/mq-golang/ibmmq@ff54c095001d81eed10615916a896512eb8d81ff
go: cannot use path@version syntax in GOPATH mode
root@host:/# cd myproject/
root@host:/myproject# ls go.mod 
go.mod
root@host:/myproject# go get github.com/ibm-messaging/mq-golang/ibmmq@ff54c095001d81eed10615916a896512eb8d81ff
go: finding github.com ff54c095001d81eed10615916a896512eb8d81ff
go: finding github.com/ibm-messaging/mq-golang/ibmmq ff54c095001d81eed10615916a896512eb8d81ff
go: finding github.com/ibm-messaging/mq-golang ff54c095001d81eed10615916a896512eb8d81ff
go: finding github.com/ibm-messaging ff54c095001d81eed10615916a896512eb8d81ff
<</div> div class="one_answers">

当我尝试在初始化 go mod 的目录之外的目录中运行命令时遇到了这个问题。为了下载具有特定版本的模块,go需要go.mod文件,该文件可以跟踪同一模块的多个版本。但是,尝试在go模块目录(将引用GOPATH来存储下载模块)之外的任何其他位置下载模块将失败,因为没有选项可以跟踪同一模块的不同版本。

最新更新