Circular Import in Go

How to Fix "import cycle not allowed"

Resolve Go import cycles by extracting shared code into a new, independent package that both conflicting packages can import.

Fix the error by moving shared types or functions into a separate package that both original packages can import without importing each other. Create a new file shared/types.go containing the common code, then update package A and package B to import your/module/shared instead of each other.

// shared/types.go
package shared

type Node struct {
    Circular bool
}
// package A
package a
import "your/module/shared"

func UseNode() shared.Node { return shared.Node{} }
// package B
package b
import "your/module/shared"

func CheckNode(n shared.Node) bool { return n.Circular }