95

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
3
  • 3
    Have you tried go get github.com/user/repo?
    – mkopriva
    Jan 29, 2019 at 7:24
  • 1
    The issue was with the repo format, instead of git@github<user/repo> i had to use github.com/<user>/<repo>
    – zero
    Jan 29, 2019 at 7:25
  • sync you go mod it will fix the issue . Make sure not to repeat the dependency May 14, 2021 at 8:25

7 Answers 7

65

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

3
  • 2
    I'm not sure this actually solves the issue?! I mean, this env variable disables "GOPATH mode" (which is disabled by default anyway starting with Go v1.13), but git@github<user/repo> still isn't "path@version syntax", so it still wouldn't work?
    – rob74
    May 10, 2020 at 12:47
  • 1
    Google must have enabled it by default in 1.14 or 1.15 because I get this error message with 1.15. I do see there is a pull request to fix this yet again in 1.16. This things changes way to frequently to keep up.
    – John G
    Feb 11, 2021 at 17:12
  • Actually, since go version 1.16 the GO111MODULE=on is set by default. Nov 22, 2021 at 13:30
30

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.

0
21

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
10

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.

1
7

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

This worked for me!

1
6

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
1
  • This works if you are using a newer version of Go (1.14+) and your legacy Go project is not ready to move into go modules yet. It's a nice way to install a tool globally. But you have to use go install instead, and make sure that echo $GOBIN prints something like ~/go/bin (and that GOBIN is in your PATH), so it still works like it used to in older versions of Go
    – tothemario
    Sep 15, 2021 at 21:38
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.