Any good advice about how to avoid import cycle in Go?

27,888

Solution 1

go list -f '{{join .Deps "\n"}}' <import-path>

Will show import dependencies for package at <import-path> - or in current directory if <import-path> is left empty. Alternatively

go list -f '{{join .DepsErrors "\n"}}' <import-path>

hopefully shows some useful information in your case. See also the output of

go help list

for additional information about the go list tool.

Solution 2

To complement on jnml's answer (which helps "debug" circular references problems), you can use dependency inversion to break those cycles, coupled with dependency injection. For an application, I always try to follow the guidelines of the Clean Architecture - see here for a Go-specific example - and I find that Go's "non-declarative implementation" of interfaces (that is, you don't have to explicitly say type MyStruct struct implements IfceSomething) makes this very simple.

So, if you have packages A -> B -> C -> A, you create InterfaceA (some relevant name, obviously, more behaviour-related than package-related :) in package C and make it depend on this interface instead of on package A, and you make sure package A "implements" this interface.

Then you just have to provide a concrete implementation of A to C at some point (many possibilities here, I usually do this "glue" code in the main package that knows about all dependencies).

Solution 3

Since import relationship just become more and more complex while code grows, I'm eager to know how to avoid import cycle more efficiently in Go.

Another option is to visualize the dependencies in your project. This can be done with CLI tool godepgraph. You can install it with:

go get -u github.com/kisielk/godepgraph

And then use it for finding import cycles in your app with help of another CLI tool graphvis. Having this tools you can visualize package dependencies:

godepgraph -s path/to/my/package | dot -Tpng -o godepgraph.png
open ./godepgraph.png

to find cycles in my code: enter image description here

Share:
27,888

Related videos on Youtube

Shane Hou
Author by

Shane Hou

A Man With A Plan.

Updated on July 09, 2022

Comments

  • Shane Hou
    Shane Hou almost 2 years

    I'm working on a Go project for a month. The good thing is Go is really highly efficient. But after a month of development I've already got thousands lines of code and many packages. To avoid import cycle is a major issue for me that anytime I got a import cycle error, I have no idea where the problem may be at first time.

    The Go compiler also only have very simple notice that always not good enough to locate issue quickly like: main.go:7:3: import cycle not allowed. It will only help you to know which file may cause the problem but nothing more deeply. Since import relationship just become more and more complex while code grows, I'm eager to know how to avoid import cycle more efficiently in Go. Any help is much appreciated.

  • mna
    mna about 11 years
    Definitely good to know, didn't even know about go list, actually.
  • MattyW
    MattyW about 10 years
    Newer versions of go give you more information about where the import cycle is coming from