Use iter.Seq return types and the yield callback pattern to create lazy, single-use iterators that process data on demand without allocating intermediate slices.
func Lines(s []byte) iter.Seq[[]byte] {
return func(yield func([]byte) bool) {
for len(s) > 0 {
var line []byte
if i := bytes.IndexByte(s, '\n'); i >= 0 {
line, s = s[:i+1], s[i+1:]
} else {
line, s = s, nil
}
if !yield(line[:len(line):len(line)]) {
return
}
}
}
}
In Simple Terms: This pattern lets you loop over data one piece at a time without loading everything into memory first. Think of it like reading a book page by page instead of photocopying the whole book before starting. You use it when processing large datasets or streams where memory efficiency matters.
In Summary: Use the iter.Seq pattern with a yield callback to build memory-efficient, lazy iterators for processing data streams in Go.