gpt4 book ai didi

go - 什么是 npm-outdated 的 Go(mod)等价物?

转载 作者:IT王子 更新时间:2023-10-29 02:26:07 26 4
gpt4 key购买 nike

我想让我的 go.mod 依赖项保持最新。使用 Node.js,我运行 npm outdated(以及后来的 npm update)。

Go mod 最接近的是什么?

理想情况下,我会看到一份关于我的项目的过时依赖项的报告(并非全部递归)。谢谢

最佳答案

列出直接和间接依赖

这在 Go 1.11 Modules: How to Upgrade and Downgrade Dependencies 中有详细说明维基:

To view available minor and patch upgrades for all direct and indirect dependencies, run go list -u -m all.

To upgrade to the latest version for all direct and indirect dependencies of the current module:

  • run go get -u to use the latest minor or patch releases
  • run go get -u=patch to use the latest patch releases

您可以在此处阅读更多详细信息:Command go: List packages or modules .

还有一个第三方应用程序:https://github.com/psampaz/go-mod-outdated :

An easy way to find outdated dependencies of your Go projects. go-mod-outdated provides a table view of the go list -u -m -json all command which lists all dependencies of a Go project and their available minor and patch updates. It also provides a way to filter indirect dependencies and dependencies without updates.

只列出直接依赖

如果你对间接依赖不感兴趣,我们可以过滤掉。没有用于过滤掉间接依赖项的标志,但我们可以使用自定义输出格式来做到这一点。

The -f flag specifies an alternate format for the list, using the syntax of package template.

所以你可以指定一个格式作为模板文档,符合text/template .

When listing modules, the -f flag still specifies a format template applied to a Go struct, but now a Module struct:

type Module struct {
Path string // module path
Version string // module version
Versions []string // available module versions (with -versions)
Replace *Module // replaced by this module
Time *time.Time // time version was created
Update *Module // available update, if any (with -u)
Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any
GoMod string // path to go.mod file for this module, if any
Error *ModuleError // error loading module
}

type ModuleError struct {
Err string // the error itself
}

注意:这个 Module 结构是在命令 go 的内部包中定义的:https://godoc.org/cmd/go/internal/modinfo

因此,例如像以前一样列出直接和间接依赖关系,但现在还在间接依赖关系之后附加一个 IAMINDIRECT 词,可以这样完成:

go list -u -m -f '{{.}}{{if .Indirect}} IAMINDIRECT{{end}}' all

否定逻辑,列出直接和间接依赖关系,但这次只用IAMDRECT“标记”直接依赖关系:

go list -u -m -f '{{.}}{{if not .Indirect}} IAMDIRECT{{end}}' all

我们快到了。我们现在只需要过滤掉不包含 IAMDRECT 单词的行:

go list -u -m -f '{{.}}{{if not .Indirect}} IAMDIRECT{{end}}' all | grep IAMDIRECT

备选

上述解决方案基于 grep 命令。但实际上我们并不需要那样。如果指定的模板生成空文档,则从输出中跳过该行。

所以我们可以这样实现:

go list -u -m -f '{{if not .Indirect}}{{.}}{{end}}' all

基本上,如果不是间接的,我们只调用 Module.String()(我们只包含一个依赖项)。作为额外的收获,此解决方案也适用于 Windows。

只列出有更新的依赖

与我们过滤掉间接依赖的方式类似,这也是“小菜一碟”,因为 Module 结构包含一个 Update 字段,用于有更新的包/模块:

go list -u -m -f '{{if .Update}}{{.}}{{end}}' all

另见相关问题:How to list installed go packages

关于go - 什么是 npm-outdated 的 Go(mod)等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55866604/

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