go
build_error
ai_generated
true
不允许导入循环:包 main 导入了自身
import cycle not allowed: package main imports itself
ID: go/self-import-cycle
90%修复率
88%置信度
1证据数
2023-02-28首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Go 1.18 | active | — | — | — |
| Go 1.19 | active | — | — | — |
| Go 1.20 | active | — | — | — |
| Go 1.21 | active | — | — | — |
| Go 1.22 | active | — | — | — |
根因分析
一个包直接或间接地导入了自身,造成了 Go 编译器无法解决的依赖循环。
English
A package directly or indirectly imports itself, creating a dependency cycle that the Go compiler cannot resolve.
官方文档
https://go.dev/doc/faq#cyclic_imports解决方案
-
将共享的类型或函数提取到第三个包中,让原始的两个包都导入该包
-
使用接口反转依赖:在一个包中定义接口,在另一个包中实现它
无效尝试
常见但无效的做法:
-
90% 失败
Files in the same package cannot import the package; they share the same namespace.
-
85% 失败
Blank import still triggers the import mechanism and does not resolve the cycle.