两级以上导入循环走

Import cycles more than two levels go

所以我有这个导入周期要解决,我的项​​目结构基本上是这样的:

model.go -> procedure.go -> function.go

在我的函数中我需要模型并且我使用接口来处理它。目前我的代码基本上是这样的:

type imodel interface {
    foo()
}

type model struct {
}

func (m *model) run() {
    proc := &procedure{}
    proc.run(m)
}

func (m *model) foo() {
    //bla bla
}

type procedure struct {
}

func (p *procedure) run(model imodel) {
    funct := &function{}
    funct.run(model)
}

type function struct {
}

func (f *function) run(model imodel) {
    model.foo()
}

我的问题是我应该像这样每隔 class 使用接口彻底传递我的模型还是有任何其他解决方法?

我会将所有这些放在同一个包中。根据情况,我可能会将它们放在同一个包中的不同文件中。

此外,您似乎没有导出 imodel,因此它将是包内部的,除非您有多个具体实现,否则不需要接口。然后,“imodel”不是一个理想的名称,接口应该命名为 model 并且每个实现接口的具体类型都应该以它所建模的命名。