需要帮助解决循环依赖问题。

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

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.

huangapple
  • 本文由 发表于 2022年9月29日 16:27:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/73892512.html
匿名

发表评论

匿名网友

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

确定