go build -mod=mod with no go.mod

huangapple go评论105阅读模式
英文:

go build -mod=mod with no go.mod

问题

如果我运行类似以下命令的命令:

go build -mod=mod -o xyz.exe github.com/some/go/tool

并且我在一个包含 go.mod 文件的目录中,我注意到 go.mod 文件将被更新,引用了 github.com/some/go/tool,并且将会构建 xyz.exe。所以使用的 go.mod 是当前目录中的那个。

然而,如果我切换到一个空的临时文件夹,没有 go.mod 文件,构建仍然成功,并且不会生成或更新 go.mod。

我的问题是:在后一种情况下,使用的是哪个 go.mod?go 是否使用来自 github.com/some/go/tool 的那个?这个是否有文档记录?

英文:

If I run a command similar to the following:

go build -mod=mod -o xyz.exe github.com/some/go/tool

And I'm in a directory with a go.mod, I noticed that the go.mod will be updated with a reference to github.com/some/go/tool, and xyz.exe will be built. So the go.mod being used is the one in the current directory.

However, if I change to some empty tmp folder with no go.mod, the build still succeeds and no go.mod is generated or updated.

My question is: in the latter case, what go.mod is used? Does go use the one from github.com/some/go/tool? And, is this documented somewhere?

答案1

得分: 3

当你在一个已存在 go.mod 文件的目录中运行命令 go build -mod=mod -o xyz.exe github.com/some/go/tool 时,Go 会使用当前目录中的 go.mod 文件,并将构建 github.com/some/go/tool 包时添加的任何新依赖项添加到该 go.mod 文件中。

然而,当你在一个没有 go.mod 文件的目录中运行该命令时,Go 仍然能够构建该包,但不会在当前目录中创建或更新 go.mod 文件。

当 Go 构建一个不在当前模块中的包(当前目录的 go.mod 所定义的模块),如果存在,它将使用正在构建的包的 go.mod 文件。在这种情况下,github.com/some/go/tool 应该有自己的 go.mod 文件,并且在构建和解析依赖项时将使用该文件。

Go 的这种行为在 go help build 文档的 "Modules" 部分中有详细说明。

https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more

英文:

When you run the command go build -mod=mod -o xyz.exe github.com/some/go/tool in a directory with an existing go.mod file, Go will use the go.mod in the current directory, and any new dependencies added as a result of building the github.com/some/go/tool package will be added to that go.mod file.

However, when you run the command in a directory without a go.mod file, Go will still be able to build the package, but it will not create or update a go.mod file in the current directory.

When Go is building a package that is not in the current module (the module defined by the current directory's go.mod), it will use the go.mod file of the package that you are building if present. In this case github.com/some/go/tool should have it's own go.mod file and it will be used when building and resolving the dependencies.

This behavior of Go is specified in the go help build documentation, under the section "Modules" specifically.

https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more

答案2

得分: 1

让我们来看看go.mod的作用:

以Windows操作系统为例:

go mod文件如下:

module xxx

go 1.19

require github.com/mitchellh/mapstructure v1.5.0

如果你运行以下命令:

go build -mod=mod  xxx

它将会下载mapstructureC:\Users\{username}\go\pkg\mod\github.com\mitchellh\mapstructure@v1.5.0路径下。

接下来,如果你删除go.mod文件,\go\pkg\...\mapstructure仍然存在。

当运行或构建时,go会查找\go\pkg\go Root

因此,即使没有go.mod文件,它仍然可以构建和运行。

如果在本地删除\go\pkg\...\mapstructure,则无法运行或构建。

go mod可以在不同的环境(不同的计算机/操作系统)中进行一致的构建,并处理不同版本和包依赖关系。

英文:

let's take a look at what go.mod does:

Take windows-os as an example:

go mod file like:

module xxx

go 1.19

require github.com/mitchellh/mapstructure v1.5.0

if you run

go build -mod=mod  xxx

it will be download mapstructure in
C:\Users\{username}\go\pkg\mod\github.com\mitchellh\mapstructure@v1.5.0

Next, if you delete the go.mod, \go\pkg\...\mapstructure still exists.

go will look up \go\pkg\ and go Root, when go run/build.

so it can build/run even there is no go.mod

if delete \go\pkg\...\mapstructure in local, it will can't run/build

go mod can build consistently in different environments(different pc/os).and deal different version and package dependencies

huangapple
  • 本文由 发表于 2023年1月12日 07:54:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75090216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定