Fixing import cycle in Go

13,597

An import cycle indicates a fundamentally faulty design. Broadly speaking, you're looking at one of the following:

  • You're mixing concerns. Perhaps view shouldn't be accessing action.Register at all, or perhaps action shouldn't be responsible for changing the names of views (or both). This seems the most likely.
  • You're relying on a concretion where you should be relying on an interface and injecting a concretion. For example, rather than the view accessing action.Register directly, it could call a method on an interface type defined within view, and injected into the View object when it is constructed.
  • You need one or more additional, separate packages to hold the logic used by both the view and action packages, but which calls out to neither.

Generally speaking, you want to architect an application so that you have three basic types of packages:

  1. Wholly self-contained packages, which reference no other first-party packages (they can of course reference the standard library or other third-party packages).
  2. Logic packages whose only internal dependencies are of type 1 above, i.e., wholly self-contained packages. These packages should not rely on each other or on those of type 3 below.
  3. "Wiring" packages, which mostly interact with the logic packages, and handle instantiation, initialization, configuration, and injection of dependencies. These can depend on any other package except for other type 3 packages. You should need very, very few packages of this type - often just one, main, but occasionally two or three for more complex applications.
Share:
13,597
flooblebit
Author by

flooblebit

Updated on June 04, 2022

Comments

  • flooblebit
    flooblebit almost 2 years

    So I have this import cycle which I'm trying to solve. I have this following pattern:

    view/
    - view.go
    action/
    - action.go
    - register.go
    

    And the general idea is that actions are performed on a view, and are executed by the view:

    // view.go
    type View struct {
        Name string
    }
    
    // action.go
    func ChangeName(v *view.View) {
        v.Name = "new name"
    }
    
    // register.go
    const Register = map[string]func(v *view.View) {
        "ChangeName": ChangeName,
    }
    

    And then in view.go we invoke this:

    func (v *View) doThings() {
        if action, exists := action.Register["ChangeName"]; exists {
            action(v)
        }
    }
    

    But this causes a cycle because View depends on the Action package, and vice versa. How can I solve this cycle? Is there a different way to approach this?

  • flooblebit
    flooblebit almost 6 years
    Indeed, I'm asking what my design error is though and how I can approach it differently
  • Bert Verhees
    Bert Verhees almost 6 years
    Read the second part of my answer, there is how to approach this problem.
  • flooblebit
    flooblebit almost 6 years
    I know that I can put them in the same package. I'd rather create a more well thought out system where I can decouple both of these rather than cramming them in the same package
  • Bert Verhees
    Bert Verhees almost 6 years
    There is no other way, structs which point to each other must be in the same package.
  • flooblebit
    flooblebit almost 6 years
    Perfect, this gives me some food for thought.
  • JamMaster
    JamMaster over 4 years
    @BertVerhees And by the way, Go actually is the only language that does this. In both C++ and python you can import single files. This is an enormous difference to having to import entire packages. But maybe its just my old mindset, coming from other languages. So far I havent found a solution except for this, which is not ideal.
  • Melardev
    Melardev about 4 years
    I don't agree with that, what if you need to have an application struct or a context struct that have a reference to a NetClientService in the net package and an IOHandler in the io package, what should we do, put everything together in the same package? it does not seem correct to me. In Python and C++ There are workarounds for this unavoidable scenario(forward declaration, and reference by class name(string) at least in Django)
  • Readren
    Readren about 3 years
    I unintentionally downvoted this answer and now I have to wait some edition to undo my mistake. Sorry.