Range-over-function iterators in Go 1.23+ enable custom iteration by passing a yield-based function directly to a range loop.
Range-over-function iterators in Go 1.23+ allow you to iterate over custom sequences by passing a function that yields values to a range loop. You define a function that accepts a yield callback and call it inside the range statement to process each yielded item.
func CountTo(n int) func(func(int) bool) {
return func(yield func(int) bool) {
for i := 1; i <= n; i++ {
if !yield(i) {
return
}
}
}
}
for v := range CountTo(5) {
fmt.Println(v)
}
Range-Over-Function Iterators in Go let you write custom loops that look like standard for ... range loops but run your own logic to generate values. Instead of manually calling a function in a while loop, you pass a function to range that feeds values one by one. It works like a generator in other languages, making it easier to create reusable iteration patterns without writing boilerplate code.