Using "go get" to download binaries without adding them to go.mod

AhmetB - Google picture AhmetB - Google · Jul 1, 2019 · Viewed 8k times · Source

I'm using Go modules in my project and in my build system (e.g. Travis CI) I'm downloading a command-line utility (written in Go) with go get to assist with my build process, e.g.:

go get github.com/mitchellh/gox

However, this go get causes the file to be added to my go.mod file. This is contaminating the build environment, causing it to be "dirty" (since there are changes to some files tracked in git, in this case go.mod and go.sum), and I use git describe --always --dirty --tag to describe my build, which shows up as "dirty".

Is there a way to "go get" a binary just to download it, without adding it to the go.mod/go.sum?

I've tried setting GOPATH to somewhere else, even then, go get updates the go.mod/go.sum to add this as an // indirect dependency.

dir="$(mktemp -d)"; \
  env GOPATH="$dir" go get github.com/mitchellh/gox && \
  mv "$dir/bin/gox" "$(go env GOPATH)"/bin/gox

Answer

nishanthshanmugham picture nishanthshanmugham · Jan 15, 2021

Go 1.16 onwards

Go 1.16 (released February 2021) includes a change that makes it possible to install a binary without affecting go.mod.

Issue 40276 tracks the proposal:

cmd/go: 'go install' should install executables in module mode outside a module

This was implemented in CL 254365. As part of this change, if I understand correctly, you can run:

go install github.com/foo/bar@latest 

to install a command without affecting go.mod.