为了账号安全,请及时绑定邮箱和手机立即绑定

为什么自 Go 1.17 以来,go.mod 中有两个“需要”块?

为什么自 Go 1.17 以来,go.mod 中有两个“需要”块?

Go
慕桂英4014372 2022-10-04 18:51:24
我创建了一个小型 go 应用程序。几天前,我已经从go 1.15升级到1.17,我还用升级了软件包。更改后,我的go.mod文件中有2个需要块。这是为什么呢?这是什么意思?还好还是有什么东西坏了?go get -u应用程序仍可正确生成。文件:module github.com/jozo/simple-pomodorogo 1.17require (    fyne.io/fyne/v2 v2.1.0    github.com/dsnet/golib/memfile v1.0.0    github.com/faiface/beep v1.1.0    github.com/fsnotify/fsnotify v1.5.1 // indirect    github.com/go-gl/gl v0.0.0-20210905235341-f7a045908259 // indirect    github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be // indirect    github.com/godbus/dbus/v5 v5.0.5 // indirect    github.com/hajimehoshi/oto v1.0.1 // indirect    github.com/srwiley/oksvg v0.0.0-20210519022825-9fc0c575d5fe // indirect    github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect    github.com/yuin/goldmark v1.4.1 // indirect    golang.org/x/exp v0.0.0-20210916165020-5cb4fee858ee // indirect    golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect    golang.org/x/mobile v0.0.0-20210924032853-1c027f395ef7 // indirect    golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6 // indirect    golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect    golang.org/x/text v0.3.7 // indirect    gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect)require (    github.com/davecgh/go-spew v1.1.1 // indirect    github.com/fredbi/uri v0.0.0-20181227131451-3dcfdacbaaf3 // indirect    github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff // indirect    github.com/pkg/errors v0.9.1 // indirect    github.com/pmezard/go-difflib v1.0.0 // indirect    github.com/stretchr/testify v1.7.0 // indirect)
查看完整描述

1 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

因为在 Go 1.17 中,模块图已更改为启用修剪和延迟加载。第二个块包含间接依赖关系。require

https://golang.org/doc/go1.17#go-command

如果模块指定 go 1.17 或更高版本,则模块图仅包含其他 go 1.17 模块的直接依赖关系,而不包括其完全的传递依赖关系。[...]

[...]如果模块在其 go.mod 文件中指定 go 1.17 或更高版本,则其 go.mod 文件现在包含一个显式 require 指令,用于提供可传递导入包的每个模块。(在以前的版本中,go.mod 文件通常只包含对直接导入的包的显式要求。

由于在扩展的 Go 1.17 go.mod 文件中,显式需求的数量可能要大得多,因此 go 1.17 模块中对间接依赖项的新添加需求将保留在与包含直接依赖项的块不同的需求块中。

注意:您在问题中发布的文件在第一个 require 块中具有依赖项。根据引用的文档中的“新添加”一词,我怀疑这是因为这些依赖项已经在那里列出并且没有重新排列它们。如果您:go.mod//indirect//indirectgo mod tidy

  • 手动删除其中一个

  • 和/或重新创建 Go 版本设置为 或更高的文件go.mod1.17

  • 和/或运行go mod tidy -go=1.17

然后它将在两个块中分离直接和依赖关系。无论如何,这是一种视觉上的便利,文档并不要求创建两个单独的块。//indirect


其他参考资料:


查看完整回答
反对 回复 2022-10-04
  • 1 回答
  • 0 关注
  • 259 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号