Use the iter package functions Map, Filter, Take, and Skip to chain transformations on iterators returned by functions like bytes.Lines or bytes.FieldsSeq. These functions accept an iter.Seq[T] and return a new iter.Seq[U] that applies the logic lazily.
package main
import (
"bytes"
"fmt"
"iter"
"strings"
)
func main() {
data := []byte("line1\nline2\nline3\n")
// Chain: Lines -> Filter (contains '2') -> Map (uppercase) -> Take (1)
lines := bytes.Lines(data)
filtered := iter.Filter(lines, func(line []byte) bool {
return bytes.Contains(line, []byte("2"))
})
mapped := iter.Map(filtered, func(line []byte) string {
return strings.ToUpper(string(line))
})
limited := iter.Take(mapped, 1)
for line := range limited {
fmt.Println(line)
}
}