Go 1.23 New Features: range-over-func Finally Here
Contents
range-over-func Officially Available
// can do lazy collection without channel
func Range(start, end int) func(yield func(int) bool) {
return func(yield func(int) bool) {
for i := start; i < end; i++ {
if !yield(i) {
return
}
}
}
}
func main() {
for v := range Range(0, 10) {
fmt.Println(v)
}
}iterators Package
import "iter"
func First[T any](seq iter.Seq[T]) (T, bool) {
for v := range seq {
return v, true
}
var zero T
return zero, false
}Practical Value
range-over-func good for:
- processing large datasets (don’t need to load all in memory)
- infinite sequences
- pipeline-style data processing
Conclusion
Go 1.23 has no revolutionary changes, but range-over-func formalization is an important engineering advancement.