英文:
How can I pinpoint an "import cycle not allowed" problem?
问题
我可以通过分析哪个源文件导致了“不允许导入循环”问题来解决这个问题。错误信息并不清楚,无法让我解决问题:
package command-line-arguments
imports app.exap/i8/internal
imports app.exap/i8/internal/data/retrieves
imports app.exap/i8/internal/integration/datastore
imports app.exap/i8/internal/objects/modules
imports app.exap/i8/internal/data
imports app.exap/i8/internal/integration/datastore: import cycle not allowed
package command-line-arguments
imports app.exap/i8/internal
imports app.exap/i8/internal/data/retrieves
imports app.exap/i8/internal/integration/datastore
imports app.exap/i8/internal/objects/modules
imports app.exap/i8/internal/data
imports app.exap/i8/internal/objects/modules: import cycle not allowed
英文:
How can I analyze which source file is causing the "import cycle not allowed" problem?
The error message isn't clear enough to allow me to solve the problem:
package command-line-arguments
imports app.exap/i8/internal
imports app.exap/i8/internal/data/retrieves
imports app.exap/i8/internal/integration/datastore
imports app.exap/i8/internal/objects/modules
imports app.exap/i8/internal/data
imports app.exap/i8/internal/integration/datastore: import cycle not allowed
package command-line-arguments
imports app.exap/i8/internal
imports app.exap/i8/internal/data/retrieves
imports app.exap/i8/internal/integration/datastore
imports app.exap/i8/internal/objects/modules
imports app.exap/i8/internal/data
imports app.exap/i8/internal/objects/modules: import cycle not allowed
答案1
得分: 7
问题不能归因于单个源文件,因为导入循环通常涉及来自不同包的多个源文件。
但是,你可以从错误消息中获取有价值的信息:你的项目似乎遭受了两个不同的导入循环(一个2循环和一个3循环),涉及三个包:
为了打破这些循环,你首先需要决定应该消除循环的哪些边。然而,对于这一点我很难给你明确的指导,因为最好消除的边高度依赖于你的项目的上下文。
然而,在Go语言中避免导入循环的一个好的经验法则是高级包不应该依赖于低级包。因此,你很可能不希望app.exap/i8/internal/data
依赖于以下任何一个:
app.exap/i8/internal/integration/datastore
app.exap/i8/internal/objects/modules
一旦你决定了要消除的两个边,只需确定导入包的哪个源文件包含有问题的导入声明,并找到一种方法来重构你的代码以删除它们。
英文:
The problem cannot be ascribed to a single source file, simply because an import cycle typically involves multiple source files from different packages.
You can glean valuable information from the error message, though: your project seems to suffer from two distinct import cycles (a 2-cycle and a 3-cycle) involving three packages:
To break those cycles, you first need to decide which edges of the cycles should be eliminated. It's difficult for me to give you definite guidance about this, though, as the best edge to eliminate is highly dependent on the context of your project.
However, a good rule of thumb for avoiding import cycles in Go is that high-level packages shouldn't depend on lower-level packages. Accordingly, you most likely do not want app.exap/i8/internal/data
to depend on
- either
app.exap/i8/internal/integration/datastore
- or
app.exap/i8/internal/objects/modules
.
Once you've decided which two edges to eliminate, simply identify which source file(s) of the importing packages contain the offending import declarations and find a way to refactor your code so as to remove them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论