go
build_error
ai_generated
true
import cycle not allowed: package main imports itself
ID: go/self-import-cycle
90%Fix Rate
88%Confidence
1Evidence
2023-02-28First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Go 1.18 | active | — | — | — |
| Go 1.19 | active | — | — | — |
| Go 1.20 | active | — | — | — |
| Go 1.21 | active | — | — | — |
| Go 1.22 | active | — | — | — |
Root Cause
A package directly or indirectly imports itself, creating a dependency cycle that the Go compiler cannot resolve.
generic中文
一个包直接或间接地导入了自身,造成了 Go 编译器无法解决的依赖循环。
Official Documentation
https://go.dev/doc/faq#cyclic_importsWorkarounds
-
95% success Extract shared types or functions into a third package that both original packages import
Extract shared types or functions into a third package that both original packages import
-
90% success Use interfaces to invert dependencies: define an interface in one package and implement it in another
Use interfaces to invert dependencies: define an interface in one package and implement it in another
中文步骤
将共享的类型或函数提取到第三个包中,让原始的两个包都导入该包
使用接口反转依赖:在一个包中定义接口,在另一个包中实现它
Dead Ends
Common approaches that don't work:
-
90% fail
Files in the same package cannot import the package; they share the same namespace.
-
85% fail
Blank import still triggers the import mechanism and does not resolve the cycle.