Update: If you’re using Go 1.16, this answer still works, but go install
has changed and is now the recommended method for installing executable packages. See Karim’s answer for an explanation: https://stackoverflow.com/a/68559728/10490740
Using Go >= 1.11, if your current directory is within a module-based project, or you’ve set GO111MODULE=on
in your environment, go get
will not install packages “globally”. It will add them to your project’s go.mod file instead.
As of Go 1.11.1, setting GO111MODULE=off
works to circumvent this behavior:
GO111MODULE=off go get github.com/usr/repo
Basically, by disabling the module feature for this single command, it will install to GOPATH as expected.
Projects not using modules can still go get
normally to install binaries to $GOPATH/bin
.
There’s a lengthy conversation and multiple issues logged about this change in behavior branching from here: golang/go – cmd/go: go get should not add a dependency to go.mod #27643.