← All notes

Range over function iterators in Go 1.23

Answer: Since Go 1.23, a for ... range loop can range over a function. An iterator is a function that takes a yield callback; it calls yield for each value and stops if yield returns false.

Shapes

func(yield func() bool)
func(yield func(V) bool)
func(yield func(K, V) bool)

Example

func Count(n int) iter.Seq[int] {
    return func(yield func(int) bool) {
        for i := 0; i < n; i++ {
            if !yield(i) { return }
        }
    }
}

for i := range Count(3) { fmt.Println(i) }

When the loop body hits break, yield returns false and the iterator should stop.

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors (go.dev), Range Over Function Types - The Go Programming Language, CC BY 4.0 (go.dev text); The Go Authors (go.dev), The Go Programming Language Specification - The Go Programming Language, CC BY 4.0 (go.dev text).

Cite this

Permanent ID: iil:note:7v4xvtn0

iinventedlight. 2026. "Range over function iterators in Go 1.23." iinventedlight Library, iil:note:7v4xvtn0, version of 2026-11-04. https://iinventedlight.com/library/notes/go-range-over-func/. License: CC-BY-4.0.

@misc{iil_note_7v4xvtn0,
  author = {{iinventedlight}},
  title = {{Range over function iterators in Go 1.23}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/go-range-over-func/},
  note = {Permanent ID iil:note:7v4xvtn0. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/go-range-over-func.md.