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
- id: C1 claim: Range over function types is new in Go 1.23. source: S1 evidence: "Range over function types is a new language feature in the Go 1.23"
- id: C2 claim: By convention the callback is called yield. source: S1 evidence: "arguments and returns a bool; by convention, we call it the yield"
- id: C3 claim: Go 1.23 supports ranging over functions. source: S1 evidence: "As of Go 1.23 it now supports ranging over functions that take a"
- id: C4 claim: The spec lists func(yield func(V) bool) as a range form. source: S2 evidence: "func(yield func(V) bool)"
Sources
- [S1] Range Over Function Types - The Go Programming Language. The Go Authors (go.dev). https://go.dev/blog/range-functions . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.
- [S2] The Go Programming Language Specification - The Go Programming Language. The Go Authors (go.dev). https://go.dev/ref/spec . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.
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
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.},
}
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.