Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find where is the import cycle in go code?

Tags:

go

I am fed up with the error

main.go:10:5: import cycle not allowed

The bug does not give information, where is the dependency cycle and does not even tell which file is the problem. Is there a way I can find out the dependency cycle or dependency diagram in go? As the codebase goes large its very difficult to deduce this.

like image 703
Infant Dev Avatar asked Sep 04 '13 05:09

Infant Dev


People also ask

What is import cycle in go?

Let's say we have two packages, p1 and p2 . When package p1 depends on package p2 and package p2 depends on package p1 , it creates a cycle of dependency.

Where can I find cyclic dependency in Golang?

So what is a cyclic dependency? Let's say we have a struct A and struct B in package 'a' and 'b' respectively. struct A is dependent on struct B and struct B is dependent on struct A, then we can say there is a cyclic dependency or an import cycle in Golang.

Why is import cycle not allowed in Golang?

You cannot trick the Go compiler, and that is a good thing. This restriction has been made by design. It is supposed to ease the compilation process, and give the project a single direction.

How do imports work in go?

To import a package, we use import syntax followed by the package name. 💡 Unlike other programming languages, a package name can also be a subpath like some-dir/greet and Go will automatically resolve the path to the greet package for us as you will see in the nested package topic ahead.


1 Answers

And the collective answer based on comments above:

Its a known issue, recently fixed so look out for a new release or compile the go tool from source.

(Contributed by: @MatrixFrog, @infantDev, @jnml and @nemo)

like image 131
miltonb Avatar answered Nov 07 '22 14:11

miltonb