【问题标题】:Behaviour of "go mod tidy" vs "go build"“go mod tidy”与“go build”的行为
【发布时间】:2019-08-22 07:14:29
【问题描述】:

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

+-- app/
+-- otherstuff/
+-- test/
+-- go.mod
+-- go.sum
+-- main.go

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

# 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)

现在,如果我运行 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

我不知道这里发生了什么。我预计go mod tidy 会让go.mod 保持干净状态,因此运行go build 不会改变它。

有什么想法吗?

【问题讨论】:

  • 你运行的是当前版本的 Go 吗?
  • go version go1.12.7 linux/amd64
  • 你有机会尝试 Go 1.13 吗?

标签: go go-modules


【解决方案1】:

您所期望的行为通常是正确的。

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

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

【讨论】:

  • 我看不出这如何回答任何问题。你想确认这是 Go 中的一个错误吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 2023-02-14
  • 2013-10-14
  • 2023-02-01
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多