liveBook · Manning

Iter.Seq

As explained in Go by Example

iter.Seq is Go's generic standard-library function type representing a push iterator whose yield callback receives values and returns whether iteration should continue.

iter.Seq is Go’s generic function type for a push iterator: the iterator calls a consumer-provided yield function with each value, and the callback returns whether iteration should continue. A producer can return iter.Seq instead of a slice or channel, separating the public iteration API from its storage or execution strategy. section 6.3.1

The type supports practical iteration patterns:

  • Use a for-range loop to consume values one at a time, including values produced while work is still progressing. section 6.3.1
  • Let consumers gather every value or stop early, instead of requiring the producer to build a complete slice first. section 6.3.1

Testing an iterator consumer

A slice can be converted with slices.Values and passed through a domain-specific iterator type: Results(slices.Values(results)). This lets a test exercise Summarize with ordinary Result fixtures without manually implementing an iterator.

How Go by Example covers it

How deep it goes
Defines the callback signature, consumption model, aliases, and slice conversion, but defers deeper iterator implementation details.
Examples
Uses a Results iterator, HTTP-request result streaming, and tests converting []Result with slices.Values.
What it assumes
Assumes familiarity with Go functions and generics while introducing custom iterators and their compiler-supported range usage.
What you can do after
Apply it via for-range loops or slices.Values to expose and test sequential value streams.
How it connects
Connects push iterators with for-range loops, slices.Values, slices, channels, and concurrent execution.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.