使用"go get"下载二进制文件而不将其添加到go.mod [英] Using "go get" to download binaries without adding them to go.mod
问题描述
我在我的项目和构建系统(例如Travis CI)中使用Go模块,我正在使用go get
下载一个命令行实用程序(用Go编写)来辅助构建过程,例如:
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
但是,此go get
导致文件被添加到我的go.mod
文件中.这污染了构建环境,使其变得肮脏"(因为git中跟踪的某些文件发生了更改,在本例中为go.mod和go.sum),我使用git describe --always --dirty --tag
来描述我的构建,显示为脏".
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".
有没有一种方法可以直接下载二进制文件而无需将其添加到go.mod/go.sum中?
Is there a way to "go get" a binary just to download it, without adding it to the go.mod/go.sum?
我尝试将GOPATH设置为其他位置,即使这样,go get
也会更新go.mod/go.sum,以将其添加为// indirect
依赖项.
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
推荐答案
希望在Go 1.14中,go get
将有一个新标记,它完全符合您的要求.在问题#30515 "cmd/go:提供一致的全局安装命令中对此进行了跟踪.
Hopefully in Go 1.14 there will be a new flag for go get
that does exactly what you are asking. This is tracked in issue #30515 "cmd/go: offer a consistent global install command".
在此之前,您有几种不同的选择.
Prior to that, you have a few different options.
如果使用的是Go 1.12或更高版本,最简单的解决方案可能是在执行go get
之前将当前模块外的目录移至没有go.mod
的目录,例如:
If you are using Go 1.12 or later, the simplest solution is probably to move outside your current module to a directory without a go.mod
prior to doing the go get
, such as:
$ cd /tmp
$ go get github.com/foo/bar@v1.2.3
$ cd - # return to prior directory
转到1.11、1.12、1.13+:gobin
gobin 是可感知模块的命令,用于安装或运行二进制文件,它提供了更多的灵活性,包括无需更改当前模块的go.mod
即可安装的功能.请参见gobin
自述文件和
Go 1.11, 1.12, 1.13+: gobin
gobin is a module-aware command to install or run binaries that provides additional flexibility, including the ability to install without altering your current module's go.mod
. See the gobin
README and FAQ for more details.
如果将Go 1.11与模块一起使用,则第一步可能是升级到Go 1.12或1.13,因为模块有很多改进.如果需要使用Go 1.11并希望使用@version
语法而不更新当前模块的go.mod
,则一种方法是创建一个临时模块:
If you are using Go 1.11 with modules, the first step is probably to upgrade to Go 1.12 or 1.13 given there are many improvements in modules. If you are required to use Go 1.11 and want to use the @version
syntax without updating your current module's go.mod
, then one approach is to create a temporary module:
cd $(mktemp -d) && go mod init tempmod && go get github.com/foo/bar@v1.2.3
这是因为在Go 1.11中,除非您位于模块中,否则无法使用@version
语法,而在Go 1.12中已放松了该模块.
This is because in Go 1.11, you can't use the @version
syntax unless you are in a module, which was relaxed in Go 1.12. This approach has been automated by a simple shell script by @rogpeppe.
通常,模块-模块中的go
命令始终确定其所在的模块,该模块基于您调用go
命令时的当前工作目录. (您可以比喻没有任何args的make
如何在当前工作目录中查找makefile,或者从历史上来看,没有任何args的go build
如何建立当前工作目录,等等).
In general, the go
command in module-module always determines what module it is "in", which is based on the current working directory when you invoke the go
command. (You could make an analogy to how make
without any args will look for a makefile in the current working directory, or how historically go build
without any args will build the current working directory, etc.).
对于模块,go get
在当前工作目录或其任何父目录中查找go.mod
文件,并且go get
将使用任何go.mod
中列出的约束作为求解版本的一部分作为根据需要在执行go get
的基础上更新go.mod
的方法.这就是为什么如果在现有模块中运行go get
会更新go.mod
文件的原因.
With modules, go get
looks for a go.mod
file in the current working directory or any of its parents, and go get
will use the constraints listed in any go.mod
as part of solving for versions, as well as update the go.mod
if needed based on doing the go get
. That is why your go.mod
file is updated if you run go get
from within an existing module.
另一方面,从Go 1.12开始,如果您所在的目录不属于任何模块(即该目录没有go.mod
,也没有其任何父目录),则存在无需更新go.mod
,但是go
命令仍可以在模块模式下运行并使用@version
语法.
On the other hand, starting with Go 1.12, if you are in a directory that is not part of any module (that is, the directory does not have a go.mod
, nor do any of its parents), then there is no go.mod
to update, but the go
command is still able to operate in module mode and use the @version
syntax.
当GO111MODULE设置为on时,go命令现在支持模块目录外部的模块感知操作,前提是这些操作不需要解析相对于当前目录的导入路径或显式编辑go.mod文件.诸如go get,go list和go mod download之类的命令的行为就像在具有最初为空的要求的模块中一样.在这种模式下,go env GOMOD报告系统的空设备(/dev/null或NUL).
When GO111MODULE is set to on, the go command now supports module-aware operations outside of a module directory, provided that those operations do not need to resolve import paths relative to the current directory or explicitly edit the go.mod file. Commands such as go get, go list, and go mod download behave as if in a module with initially-empty requirements. In this mode, go env GOMOD reports the system's null device (/dev/null or NUL).
这篇关于使用"go get"下载二进制文件而不将其添加到go.mod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!