100

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
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
zero
  • 2,054
  • 2
  • 14
  • 23

7 Answers7

67

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

emaxi
  • 1,799
  • 1
  • 16
  • 13
  • 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` still isn't "path@version syntax", so it still wouldn't work? – rob74 May 10 '20 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 '21 at 17:12
  • Actually, since go version 1.16 the `GO111MODULE=on` is set by default. – elboletaire Nov 22 '21 at 13:30
34

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.

rob74
  • 4,939
  • 29
  • 31
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
cz.ycaptain
  • 313
  • 3
  • 6
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.

Feckmore
  • 4,322
  • 6
  • 43
  • 51
  • 1
    This worked for me to get buffalo to install correctly on Mac OS with golang 1.15.8 (https://gobuffalo.io/en/docs/getting-started/installation#custom-installation-with-sqlite3-support) – james-see Aug 16 '21 at 15:45
8

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

This worked for me!

  • 1
    In a more "Ubuntu way", it worked for me following this article https://computingforgeeks.com/how-to-install-latest-go-on-centos-ubuntu/ – Jean Claveau Jan 04 '22 at 10:10
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
Dr.Stein
  • 121
  • 1
  • 4
  • 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 '21 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.

vignesh
  • 498
  • 8
  • 18