I have been trying to understand the concept of iterators introduced in the new Go 1.23 release, but I’m struggling to comprehend how the iteration call happens multiple times and where the boolean value for stopping the loop is obtained. Here’s my code snippet for reference:
package main
import "fmt"
func Countdown(v int) func(func(int) bool) {
fmt.Println("v :", v) // This function runs only one time
return func(f func(int) bool) {
for i := v; i >= 0; i-- {
if !f(i) {
return
}
}
}
}
func main() {
// fmt.Println("Countdown :", Countdown(2))
for x := range Countdown(2) {
fmt.Println(x)
}
}
I appreciate any help in understanding this concept better. Thanks in advance!"