iterator

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 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 defines a legacy interface for iterating over collections of items

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(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

Jump to

Keyboard shortcuts

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