关于如何避免 Go 中的导入循环有什么好的建议吗? [英] Any good advice about how to avoid import cycle in Go?

查看:18
本文介绍了关于如何避免 Go 中的导入循环有什么好的建议吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 Go 项目上工作了一个月.好消息是 Go 确实非常高效.但是经过一个月的开发,我已经有了数千行代码和许多.避免导入周期对我来说是一个主要问题,每当我遇到导入周期错误时,我第一次不知道问题可能出在哪里.

I'm working on a Go project for a month. The good thing is Go is really highly efficient. But after a month of development I've already got thousands lines of code and many packages. To avoid import cycle is a major issue for me that anytime I got a import cycle error, I have no idea where the problem may be at first time.

Go 编译器也只有非常简单的通知,总是不足以快速定位问题,例如:main.go:7:3: import cycle not allowed.它只会帮助您了解哪个文件可能导致问题,但不会更深入.由于随着代码的增长,import 关系变得越来越复杂,我很想知道如何在 Go 中更有效地避免导入循环.非常感谢任何帮助.

The Go compiler also only have very simple notice that always not good enough to locate issue quickly like: main.go:7:3: import cycle not allowed. It will only help you to know which file may cause the problem but nothing more deeply. Since import relationship just become more and more complex while code grows, I'm eager to know how to avoid import cycle more efficiently in Go. Any help is much appreciated.

推荐答案

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

将在 <import-path> 或当前目录中显示包的导入依赖关系,如果 <import-path> 为空.或者

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

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

希望在您的案例中显示一些有用的信息.另见

hopefully shows some useful information in your case. See also the output of

go help list

有关 go list 工具的更多信息.

for additional information about the go list tool.

这篇关于关于如何避免 Go 中的导入循环有什么好的建议吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆