英文:
Need help to resolve cyclic dependency problem
问题
我正在为我的公司的一个项目工作,需要在现有代码中添加一个新的接口及其实现。在这个过程中,我遇到了一个具有循环依赖问题的挑战性情况。
以下是更多详细信息:
项目目录:test-cyclic-dependency
现有代码位于文件1中:test-cyclic-dependency/model/state/model.go
package state
import ("test-cyclic-dependency/model")
type CollectionByCollectionName struct {
CollectionName string
imOfTypeInModel model.STRING
}
我需要在文件2中定义一个新的接口Search:test-cyclic-dependency/model/search.go
package model
import (
"test-cyclic-dependency/model/state"
)
type Search interface {
Search(keyword string) state.CollectionByCollectionName
}
type STRING string
这段代码导致了循环依赖错误,错误信息如下:
# go build model/state/model.go
package command-line-arguments
imports test-cyclic-dependency/model
imports test-cyclic-dependency/model/state
imports test-cyclic-dependency/model: import cycle not allowed
有人可以帮助我解决这个问题吗?是否有一种方法可以在保持代码组织结构不变的情况下解决这个问题?
英文:
I am working on one project at my company where I need to add a new interface and it's implementation to the existing code. While I did that, I came across a challenging situation where I am hit with cyclic dependency issue.
Here are further details:
Project directory: test-cyclic-dependency
Existing code is in File1: test-cyclic-dependency/model/state/model.go
package state
import ("test-cyclic-dependency/model")
type CollectionByCollectionName struct {
CollectionName string
imOfTypeInModel model.STRING
}
I need to define a new interface Search in File2: test-cyclic-dependency/model/search.go
package model
import (
"test-cyclic-dependency/model/state"
)
type Search interface {
Search(keyword string) state.CollectionByCollectionName
}
type STRING string
This code resulting into cyclic dependency error as below:
# go build model/state/model.go
package command-line-arguments
imports test-cyclic-dependency/model
imports test-cyclic-dependency/model/state
imports test-cyclic-dependency/model: import cycle not allowed
Could someone help me to address this issue? Would there be a way to resolve it keeping the code organisation the same?
答案1
得分: 2
为了打破依赖循环,将type STRING string
移动到state
包中(因为它只在那里使用,所以我猜它与之相关),并从state
包中移除model
导入。或者,如果在逻辑上不适合放在那里,将type STRING string
移动到自己的第三个包中,并只在state
包中导入它。
注意:我假设STRING
类型只是一个占位符,用于保持你的示例简单而已。实际上没有必要使用type STRING string
。如果你将其移除并直接使用string
,则不再需要在state
中导入model
。
英文:
To break the dependency cycle move type STRING string
into the state
package (as that's only place where it is used - so I guess it is related to it) and remove model
import from state
package. Alternatively, if it does not fit there logically move type STRING string
into its own third package and only import that in the state
package.
NOTE: I assume type STRING
is just placeholder for something more meaningful to keep your example simple. There's no point in having type STRING string
at all. If you remove it and just use string
you no longer need to import model
in state
.
答案2
得分: 0
已解决,通过将搜索界面移动到一个新的独立包"search"中。
感谢大家的帮助。
谢谢和问候,
Prafulla。
英文:
Resolved it by moving the Search interface to a new separate package 'search'.
Thank you all for helping me in this.
Thanks and Regards,
Prafulla.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论