iterator

package
v0.35.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 0 Imported by: 3

README

iterator

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.

Documentation

Overview

Package iterator adapts cursor-style collections into slices, channels, and mapped results.

An Iterator yields values by filling in a pointer — Next(any) bool — which is the shape a database cursor naturally has. Slice, Channel, and Map drain one, calling a caller-supplied constructor once per element; that constructor must return a fresh value every time, because a shared value would leave every result aliasing the same item.

This is the legacy iteration interface, predating range-over-func. New code should prefer iter.Seq and the ranges package; reach for this one only to adapt a type that already implements Iterator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Channel

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

func ChannelWithCancel

func ChannelWithCancel[T any](iterator Iterator, constructor func() T, cancel <-chan bool) chan T

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

func Map

func Map[In any, Out any](it Iterator, fn func(In) Out) []Out

Map converts an iterator into a slice of items. Deprecated: freeing up this namespace to use for new Go 1.23 range functions

func Slice

func Slice[T any](iterator Iterator, constructor func() T) []T

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

Types

type Iterator

type Iterator interface {

	// Next populates the provided value with the next item, and returns FALSE when the iterator is exhausted
	Next(any) bool

	// Count returns the total number of items available to this iterator
	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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL