go build_error ai_generated true

不允许导入循环:包 main 导入了自身

import cycle not allowed: package main imports itself

ID: go/self-import-cycle

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://go.dev/doc/faq#cyclic_imports

解决方案

  1. 将共享的类型或函数提取到第三个包中,让原始的两个包都导入该包
  2. 使用接口反转依赖:在一个包中定义接口,在另一个包中实现它

无效尝试

常见但无效的做法:

  1. 90% 失败

    Files in the same package cannot import the package; they share the same namespace.

  2. 85% 失败

    Blank import still triggers the import mechanism and does not resolve the cycle.