Why This Happens
The range clause in Go works with a limited set of types: slices, arrays, maps, strings, and channels. If you try to range over a struct, integer, or other unsupported type, the compiler will reject it. You may need to convert your data to a slice first.
The Problem
type MyList struct {
items []int
}
func main() {
list := MyList{items: []int{1, 2, 3}}
for _, v := range list { // cannot range over list
fmt.Println(v)
}
}The Fix
func main() {
list := MyList{items: []int{1, 2, 3}}
for _, v := range list.items {
fmt.Println(v)
}
}Step-by-Step Fix
- 1
Identify the type
Check the type of the variable you are trying to range over.
- 2
Find the iterable field
If the variable is a struct, find the slice, map, or array field you actually want to iterate.
- 3
Range over the correct value
Change the range expression to target the iterable field or convert the data to a rangeable type.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free