Adapters that drain a cursor-style Iterator (Next(any) bool + Count() int) into a slice, channel, or mapped result. Part of rosetta.
This is the legacy iteration interface, predating Go 1.23 range-over-func. For new code using iter.Seq, see ranges; reach for this package only when adapting a type that already implements the Iterator interface (e.g. a database cursor).
What matters here
Next populates a value through a pointer, so the constructor func() T must return a FRESH value each call.Slice, Channel, and Map call the constructor once per element and pass &value to Next. If the constructor returns a shared/aliased value, every slice entry ends up pointing at the same mutated item. Allocate a new zero value each time.
Channel spawns a goroutine that closes its output and runs until the iterator is exhausted. If the consumer stops reading early, the goroutine blocks forever (leak). Use ChannelWithCancel and signal its cancel channel to stop early.
Slice pre-allocates capacity from iterator.Count(). An Iterator whose Count() lies (returns fewer than it yields) just causes a re-grow — correctness is fine, but Count() should be accurate for the allocation to help.
func Channel[T any](iterator Iterator, constructor func() T) chan T
Channel converts an Iterator into a channel of items.
You must include a constructor function that generates fully initialized values of the type you want to return.
Deprecated: freeing up this namespace to use for new Go 1.23 range functions
ChannelWithCancel converts an Iterator into a channel of items.
You must include a constructor function that generates fully initialized values of the type you want to return.
Deprecated: freeing up this namespace to use for new Go 1.23 range functions
Slice converts an Iterator into a slice of items.
You must include a constructor function that generates fully initialized values of the type you want to return.
Deprecated: freeing up this namespace to use for new Go 1.23 range functions
type Iterator interface {
Next(any) bool Count() int}
Iterator interface allows callers to iterator over a large number of items in an array/slice
Deprecated: freeing up this namespace to use for new Go 1.23 range functions