iterator

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 0 Imported by: 1 module, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 ChannelWithCancel call the constructor before every call to Next and pass &value to it. 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. Map takes no constructor: it resets its value to the zero value after each item.
  • 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 ChannelWithCancel drain one, calling a caller-supplied constructor for every element; that constructor must return a fresh value every time, because a shared value would leave every result aliasing the same item. Map takes no constructor, and decodes each item into a zero value of its input type.

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, passing each item through fn. 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 iterate over a large number of items, one at a time 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