go
type_error
ai_generated
true
无法对 func()(类型为 func() 的值)进行 range 迭代
cannot range over func() (value of type func())
ID: go/cannot-range-over-function
90%修复率
85%置信度
1证据数
2023-06-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Go 1.18 | active | — | — | — |
| Go 1.19 | active | — | — | — |
| Go 1.20 | active | — | — | — |
| Go 1.21 | active | — | — | — |
| Go 1.22 | active | — | — | — |
根因分析
尝试对函数类型使用 range 迭代,而不是对切片、映射、字符串、通道或数组使用。
English
Trying to use range on a function type instead of a slice, map, string, channel, or array.
官方文档
https://go.dev/ref/spec#For_range解决方案
-
确保函数返回一个切片或其他可迭代类型,然后对返回值进行 range 迭代
-
如果想迭代函数产生的值,使用通道(goroutine + 通道模式)
无效尝试
常见但无效的做法:
-
70% 失败
If the function returns a single value that is not iterable, the same error occurs on the return type.
-
95% 失败
Go does not allow range on interface{} either; it must be a concrete iterable type.