在 Ubuntu 16.04中得到 GOPATH 错误“ go: 在 GOPATH 模式下不能使用 path@version 语法”

无法在 $GOPATH文件夹中运行 go get git@github<user/repo>。 得到这个错误:

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

我只是想知道为什么 go get不工作,即使 $GOPATH是配置在安装过程中。环境是 ubuntu。

~/$ echo $GOPATH
/home/user/go
105595 次浏览

As you already noticed, you should use go get github.com/<user>/<repo>.

The error message you saw comes from a new feature implemented in go get to support Go modules - you can now also specify the version of a dependency: go get github.com/<user>/<repo>@<version>, where version is a git tag using semver, e.g. v1.0.2.

I had the same issue and solved setting specific env variable export GO111MODULE=on in my .zshrc(or .bashrc depending on which shell you use) and restart the shell in order to enable modules. You can find more details here: https://github.com/golang/go/wiki/Modules

I met this issue, too. After some search, the following works by using go mod instead of go get, which is a feature of Golang Modules:

$ 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

If you get this error while you trying use modules, you should change dir to project before go get:

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

I got this error with Go v1.14 when running $ go get github.com/<user>/<repo>@<version> on an empty project before I had initialized my project with modules.

To resolve, I created a go.mod file using:

$ go mod init

I was able to rerun the get command successfully, which downloaded the vendor's package, updated the go.mod file, and created a go.sum file.

Ran into this issue when i tried running the command in a directory outside of the directory where go mod is initialized. In order to download a module with a specific version go requires go.mod file which can keep track of multiple version of a same module. However trying to download the module in anywhere else outside of a go module directory(where GOPATH will be referenced to store the download module) will fail as there is no option to keep track of different versions of the same module.

Update version of go following instructions at https://gist.github.com/nikhita/432436d570b89cab172dcf2894465753

This worked for me!