Go 1.23 Recap: Iterators Arrive
You can range over functions now. Plus iterator helpers in slices and maps, the unique package, and timers that clean up after themselves.
#golang #go-recap #iterators
Go recap series — one short post per release. Released August 2024.
TL;DR
for rangeworks on functions — Go has real iterators.slicesandmapsgained iterator helpers likeslices.Sorted(maps.Keys(m)).- New
uniquepackage for cheap value interning. - Timers no longer leak if you forget
Stop().
1. Range-over-func iterators
What changed: a function shaped like func(yield func(V) bool) can be used in a for range. The iter package names it iter.Seq[V] (and iter.Seq2[K, V] for pairs).
func Count(n int) iter.Seq[int] {
return func(yield func(int) bool) {
for i := range n {
if !yield(i) {
return // caller used break
}
}
}
}
for i := range Count(3) {
fmt.Println(i) // 0, 1, 2
}
How it works: the iterator pushes values into your loop. yield returns false when the loop wants to stop.
Why it matters: one standard way to iterate over anything — trees, DB rows, paginated APIs.
2. Iterator helpers in slices and maps
m := map[string]int{"b": 2, "a": 1, "c": 3}
keys := slices.Sorted(maps.Keys(m)) // [a b c]
slices:All,Values,Backward,Collect,Sorted,Chunk, …maps:All,Keys,Values,Insert,Collect
Why it matters: "sorted keys of a map" is now one line.
3. The unique package
What changed: unique.Make(v) returns a Handle. Equal values give equal handles, and comparing handles is just a pointer check.
h1 := unique.Make("us-east-1")
h2 := unique.Make("us-east-1")
fmt.Println(h1 == h2) // true, and very cheap
Why it matters: saves memory when the same strings repeat millions of times (hostnames, tags, labels).
4. Timers that behave
- A
TimerorTickeryou forgot to stop is now garbage collected. - Timer channels are unbuffered, so after
ResetorStopyou never get a stale value.
Applies when go.mod says go 1.23 or later.
Why it matters: fewer subtle leaks and timing bugs.
Previous: ← Go 1.22 Recap · Next: Go 1.24 Recap →
Source: Go 1.23 release notes
