Howdy,

I'm trying to establish a mental model for what the compiler does with 
range functions in Go 1.23.  It looks like the compiler creates a function 
based on the for loop body, and calls it by looping over the values passed 
in.

Meaning this code:
func simpleIter(yield func(v int) bool) {
    if !yield(1) { 
        return 
    }
    if !yield(2) { 
        return 
    }
}

for x := range simpleIter {
    fmt.Println(x)
}

compiles into this code:

{
yield := func(v int) bool {
fmt.Println(v) // loop body from the code above
}
for v := 1; v <= 2; v++ {
if !yield(1) {
goto end_loop
}
}
end_loop:
}

Is that correct?

Thanks,

Justin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20e7c228-a2d6-4e87-8946-ac63fe115e6fn%40googlegroups.com.

Reply via email to