How Pull Iterators Work in Go (iter.Pull)

iter.Pull converts a Go 1.23+ iterator into a PullIterator to enable step-by-step value consumption.

iter.Pull converts a standard iterator into a PullIterator that yields values on demand by calling a provided function. It is primarily used to bridge the gap between the new iter.Seq protocol and older APIs or libraries that expect a PullIterator interface.

func main() {
    // Create a standard iterator (Go 1.23+)
    seq := func(yield func(int) bool) {
        for i := 0; i < 5; i++ {
            if !yield(i) { return }
        }
    }

    // Convert to PullIterator
    pull := iter.Pull(seq)
    defer pull.Close()

    // Consume values
    for {
        v, ok := pull.Next()
        if !ok { break }
        fmt.Println(v)
    }
}