Description
go get
is not the right command for adding a dependency to the current module. We have a command for dealing with modules, and it's go mod
, that's the command that should be used to add dependencies.
This is the first paragraph of help on go get
:
Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'.
None of that says "add the library you specified to the dependencies of the current project". And why would it? it's for getting code from somewhere else and bringing it to this machine.
Notably, go get
will even add go commands as a dependency... so like if you run go get golang.org/x/tools/cmd/goimports
and your current directory happens to be a go module, guess what? Your go.mod now has
golang.org/x/tools v0.0.0-20180911133044-677d2ff680c1 // indirect
This UX is extremely confusing for the ~2 million people who used go before modules. It's also way too implicit and magic regardless of the experience. Adding a dependency to your project should be an explicit action:
go mod add golang.org/x/tools/cmd/goimports
What did you do?
from inside a module directory
$ ls
go.mod
$ go get github.com/natefinch/lumberjack
go: finding github.com/natefinch/lumberjack latest
go: downloading github.com/natefinch/lumberjack v0.0.0-20180817145747-7d6a1875575e
~/dev/test$ more go.mod
module app
require github.com/natefinch/lumberjack v0.0.0-20180817145747-7d6a1875575e // indirect
What did you expect to see?
No change to my go.mod
What did you see instead?
go get added a library to my go.mod file.
System details
go version go1.11 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/finchnat/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/finchnat"
GOPROXY=""
GORACE=""
GOROOT="/Users/finchnat/sdk/go1.11"
GOTMPDIR=""
GOTOOLDIR="/Users/finchnat/sdk/go1.11/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
GOROOT/bin/go version: go version go1.11 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.11
uname -v: Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64
ProductName: Mac OS X
ProductVersion: 10.13.6
BuildVersion: 17G65
lldb --version: lldb-902.0.79.7
Swift-4.1