Home > Software design >  Import cycles more than two levels go
Import cycles more than two levels go

Time:12-01

So i have this import cycle to solve, and my project structure basically likes this:

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

In my function i need model and i use interface to handle it. Currently my code basically like this:

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()
}

My question is should i pass my model using interface thorough every class like that or there's any other workaround?

CodePudding user response:

I would put all of these in the same package. Depending on circumstances, I may put them in different files in the same package.

Also, you do not seem to export imodel, so it would be package-internal and unless you have multiple concrete implementations, you do not need an interface. Then, "imodel" is a less than ideal name, the interface should be named model and each concrete type implementing the interface should be named after what it models.

  • Related