“go mod tidy”与“go build”的行为

E.Z*_*.Z. 5 go go-modules

假设我有一个具有以下结构的项目:

+-- app/
+-- otherstuff/
+-- test/
+-- go.mod
+-- go.sum
+-- main.go
Run Code Online (Sandbox Code Playgroud)

我可以go.mod通过运行来确保不包含未使用的依赖项go mod tidy

# 1) Verify that no dependency containing the name "modern-go" is found on go.mod
$ grep 'modern-go' go.mod
<-- Empty (nothing found) (OK)


# 2) Run "go mod tidy", verify that nothing changes (i.e. go.mod is already clean)
$ go mod tidy -v
unused github.com/modern-go/concurrent
unused github.com/modern-go/reflect2
<-- messages above are displayed, but go.mod did not change


# 3) Verify that go.mod did not change
$ grep 'modern-go' go.mod
<-- Empty (nothing found) (OK)
Run Code Online (Sandbox Code Playgroud)

现在,如果我运行go buildgo.mod会更新:

# 4) Run "go build"
$ go build


# 5) go.mod was updated by "go build":
$ grep 'modern-go' go.mod
    github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    github.com/modern-go/reflect2 v1.0.1 // indirect
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚这里发生了什么。我预计go mod tidygo.mod保持干净的状态,go build因此运行不会改变它。

有任何想法吗?

typ*_*182 -1

您\xe2\x80\x99 所期望的行为通常是正确的。

\n\n

Go 1.13 在这方面进行了一些修复。我建议尝试这样做:

\n\n

https://twitter.com/golang/status/1164276194524762113

\n