gpt4 book ai didi

go - `go install` 在 `go.mod` 添加记录

转载 作者:行者123 更新时间:2023-12-01 21:20:10 25 4
gpt4 key购买 nike

我对 Go 模块如何使用 go install 安装二进制文件感到有些困惑。 .

我尝试通过执行 go install github.com/joho/godotenv/cmd/godotenv 来安装 ( https://github.com/joho/godotenv ) 二进制文件我发现它在 go.mod 中添加了一条记录.
我迷路了,因为我们没有在代码中使用这个包并且在运行 go mod tidy 之后它被删除(因为它不是代码)。

有人可以解释一下 go 模块的预期行为吗?
其次,如何避免将其添加到 go.mod因为我们只需要安装和执行二进制文件?

谢谢。

Go 版本:go version go1.13.4 darwin/amd64

最佳答案

Command go: The go.mod file:

The go command automatically updates go.mod each time it uses the module graph, to make sure go.mod always accurately reflects reality and is properly formatted.



go工具将更新 go.mod当它在执行构建时检测到依赖项不准确时自动执行。

当您安装 github.com/joho/godotenv/cmd/godotenv从您的模块中,此安装至少需要构建/安装有问题的包(以及它的依赖项,可传递)。

您可以安全地运行 go mod tidy撤消对这种“一次性”依赖项的记录。

一般来说,如果你想禁止 go 工具更新 go.mod文件,您可以使用 -mod=readonly标志,但这会失败 go install (“无法加载包:包 xxx:-mod=readonly 禁用了导入查找”)。您可以在此处阅读更多信息: Go Wiki: Go modules: Can I control when go.mod gets updated and when the go tools use the network to satisfy dependencies?

或者,如果您想避免这种情况,请在模块之外构建/安装您的工具。您可以为此使用“虚拟”模块。

关于go - `go install` 在 `go.mod` 添加记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59876843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com