How can I list all the import cycles in a Go project?

huangapple go评论73阅读模式
英文:

How can I list all the import cycles in a Go project?

问题

我正在重构一个Go项目,之前所有的内容都在一个单一的模块中,现在我正在拆分出子包。在这样做之后,出现了许多导入循环。为了让项目能够再次编译,我必须消除这些循环。然而,尽管我认为已经消除了一个循环,但导入循环的计数似乎仍然保持不变。我使用以下命令来计算循环的数量:

go build 2>&1 | grep package | wc -l

即使在我进行了看起来应该消除了一个循环的工作之后,甚至在列表中不再看到我删除的循环之后,它仍然报告为6。这让我怀疑它是否只报告了一部分问题。

我还尝试了以下命令:

go build -gcflags="-e" 2>&1 | grep package | wc -l

如https://menno.io/posts/showing-all-go-compiler-errors/中所建议的,但结果相同。

英文:

I'm refactoring a Go project where everything was in a single module, splitting out sub packages. Having done that, there are many import cycles. In order for the project to compile again, I have to eliminate them. However, after doing a change that I think has eliminated a cycle, the import cycle count seems to remain the same. I count the cycles with this command:

go build 2>&1 | grep package | wc -l

It keeps reporting 6 even after I do work that seems like it should have removed a cycle, and even after I no longer see the cycle I removed in the list. It makes me wonder if it's only reporting a subset of the issues.

I also tried

go build -gcflags="-e" 2>&1 | grep package | wc -l

as suggested at https://menno.io/posts/showing-all-go-compiler-errors/, but it gave the same result.

答案1

得分: 5

如何列出Go项目中的所有导入循环?

你可以尝试使用以下命令:

go list -f '{{join .Deps "\n"}}' <import-path>

这将显示位于 <import-path> 的包的导入依赖关系,如果 <import-path> 为空,则默认为当前目录。另外,你还可以尝试以下命令:

go list -f '{{join .DepsErrors "\n"}}' <import-path>

希望这些命令能在你的情况下提供一些有用的信息。

此外,还有一个工具叫做 godepgraph,你可以在这里找到它:https://github.com/kisielk/godepgraph

安装方式如下:

go get github.com/kisielk/godepgraph

基本用法如下:

godepgraph -s path/to/my/package

How can I list all the import cycles in a Go project?

英文:

> How can I list all the import cycles in a Go project?

You can try with

go list -f &#39;{{join .Deps &quot;\n&quot;}}&#39; &lt;import-path&gt;

Will show import dependencies for package at <import-path> - or in current directory if <import-path> is left empty. Alternatively

go list -f &#39;{{join .DepsErrors &quot;\n&quot;}}&#39; &lt;import-path&gt;

hopefully shows some useful information in your case.

Also, there's godepgraph: https://github.com/kisielk/godepgraph

go get github.com/kisielk/godepgraph

Basic usage

godepgraph -s path/to/my/package

How can I list all the import cycles in a Go project?

huangapple
  • 本文由 发表于 2021年12月20日 02:51:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/70414409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定