← All notes

Go iter: Seq, Seq2, and pull-style iterators over sequences

Answer: The iter package provides basic definitions and operations for iterators over sequences.

Push iterators: Seq and Seq2

An iterator hands successive sequence elements to a callback, conventionally named yield.

Iteration stops when the sequence is finished, or when yield returns false to request an early stop.

Seq and Seq2 are pronounced like seek, the first syllable of sequence. They pass one or two values per element to yield.

Seq2 is for paired values, usually key-value or index-value pairs.

Yield returns true to continue and false to stop. Calling it again after a false return panics.

Iterator functions are most often driven by a range loop.

On a collection type, that method is conventionally named All, because it walks every value in the collection.

Single-use iterators typically report values from a data stream that cannot be rewound. Functions that return them should document that fact.

Pull, standard library use, and mutation

Pull converts a push-style Seq into a pull-style iterator accessed through next and stop.

Next returns the next value and a boolean saying whether it is valid. After the sequence ends, next returns the zero value and false.

Stop ends the iteration. Call it when you no longer want next values and next has not yet signaled the end. Callers typically defer stop(). Using next or stop from multiple goroutines at once is an error.

Pull2 likewise converts a Seq2 push iterator into a pull iterator accessed by next and stop.

A few standard library packages expose iterator APIs, notably maps and slices, such as maps.Keys and slices.Sorted.

Iterators provide only the values of the sequence, not a direct way to modify it. To allow modification during iteration, define a position type.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, iter package; The Go Authors, iter.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors, iter package; The Go Authors, iter.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.

Cite this

Permanent ID: iil:note:65pgj5mf

iinventedlight. 2026. "Go iter: Seq, Seq2, and pull-style iterators over sequences." iinventedlight Library, iil:note:65pgj5mf, version of 2026-11-27. https://iinventedlight.com/library/notes/go-iter/. License: CC-BY-4.0.

@misc{iil_note_65pgj5mf,
  author = {{iinventedlight}},
  title = {{Go iter: Seq, Seq2, and pull-style iterators over sequences}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-27},
  url = {https://iinventedlight.com/library/notes/go-iter/},
  note = {Permanent ID iil:note:65pgj5mf. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-iter.md.