Getting GOPATH error "go: cannot use path@version syntax in GOPATH mode" in Ubuntu 16.04

GoGopath

Go Problem Overview


I'm unable to run go get git@github<user/repo> in my $GOPATH folder. Getting this error:

>go: cannot use path@version syntax in GOPATH mode

I just want to understand why go get isn't working even though $GOPATH is configured during the installation. Environment is ubuntu.

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

Go Solutions


Solution 1 - Go

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

Solution 2 - Go

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.

Solution 3 - Go

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

Solution 4 - Go

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.

Solution 5 - Go

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

Solution 6 - Go

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

This worked for me!

Solution 7 - Go

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzeroView Question on Stackoverflow
Solution 1 - GoemaxiView Answer on Stackoverflow
Solution 2 - Gorob74View Answer on Stackoverflow
Solution 3 - Gocz.ycaptainView Answer on Stackoverflow
Solution 4 - GoFeckmoreView Answer on Stackoverflow
Solution 5 - GoDr.SteinView Answer on Stackoverflow
Solution 6 - GoShankara NarayanaView Answer on Stackoverflow
Solution 7 - GovigneshView Answer on Stackoverflow