假設(shè)我有一個具有以下結(jié)構(gòu)的項(xiàng)目:+-- app/+-- otherstuff/+-- test/+-- go.mod+-- go.sum+-- main.go我可以通過運(yùn)行以下命令來確保go.mod不包含未使用的依賴項(xiàng)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 -vunused github.com/modern-go/concurrentunused 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)現(xiàn)在,如果我運(yùn)行g(shù)o build,go.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我不明白這是怎么回事。我預(yù)計go mod tidy會保持go.mod干凈的狀態(tài),go build因此運(yùn)行不會改變它。有任何想法嗎?
“go mod tidy”與“go build”的行為
ibeautiful
2023-07-17 13:53:25