Learn Go 1.22+ range over functions, custom iterator signatures, range over integers, and the iter package.
0 / 5 completed
1 / 5
What does range over function introduced in Go 1.22 enable?
Range over func: Go 1.22 allows for v := range myIterator { ... } where myIterator is a function of type func(yield func(V) bool). The iterator calls yield for each value; returning false from yield signals the iterator to stop, like break.
2 / 5
What are the three iterator function signatures accepted by for range in Go 1.22+?
Iterator signatures: the compiler accepts func(func() bool) for no loop variables (like for range n), func(func(V) bool) for one variable, and func(func(K, V) bool) for two. This mirrors the existing range forms for slices, maps, and channels.
3 / 5
What does range over an integer (for i := range 5) do in Go 1.22?
Range over int:for i := range 5 { fmt.Println(i) } prints 0 through 4. This finally provides a concise idiom for simple count-based loops, replacing the verbose three-clause for loop for cases where only the iteration count matters.
4 / 5
How does a custom iterator in Go 1.22 signal early termination?
Early termination: the yield function's boolean return mirrors whether the loop wants more values. A well-behaved iterator checks if !yield(v) { return } after each call. This allows break and return inside range-over-func loops to work correctly without goroutine leaks.
5 / 5
What package in Go's standard library (Go 1.23+) provides iterator adapters for the new range semantics?
iter package: Go 1.23 added the iter package with type aliases Seq[V] and Seq2[K, V] for the iterator function types. The slices and maps packages gained All(), Values(), and other functions returning these iterator types.