go
type_error
ai_generated
true
cannot range over func() (value of type func())
ID: go/cannot-range-over-function
90%Fix Rate
85%Confidence
1Evidence
2023-06-20First 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
Trying to use range on a function type instead of a slice, map, string, channel, or array.
generic中文
尝试对函数类型使用 range 迭代,而不是对切片、映射、字符串、通道或数组使用。
Official Documentation
https://go.dev/ref/spec#For_rangeWorkarounds
-
95% success Ensure the function returns a slice or another iterable type, then range over the return value
Ensure the function returns a slice or another iterable type, then range over the return value
-
85% success Use a channel if you want to iterate over values produced by a function (goroutine + channel pattern)
Use a channel if you want to iterate over values produced by a function (goroutine + channel pattern)
中文步骤
确保函数返回一个切片或其他可迭代类型,然后对返回值进行 range 迭代
如果想迭代函数产生的值,使用通道(goroutine + 通道模式)
Dead Ends
Common approaches that don't work:
-
70% fail
If the function returns a single value that is not iterable, the same error occurs on the return type.
-
95% fail
Go does not allow range on interface{} either; it must be a concrete iterable type.