Help improve this documentation
This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Iterator - Channel helpersโ
This page lists all operations on channels, available in the it
lo sub-package.
SeqToChannelโ
Converts an
iter.Seq
(oriter.Seq2
) into a read-only channel. Items are sent on a buffered channel and the channel is closed when the sequence ends.Examples:
// SeqToChannel: stream ints from a sequence
seq := it.Range(5) // 0..4
ch := it.SeqToChannel(2, seq)
var got []int
for v := range ch {
got = append(got, v)
}
// got == []int{0, 1, 2, 3, 4}// SeqToChannel2: stream key/value pairs as Tuple2
m := map[string]int{"a": 1, "b": 2}
kv := it.Entries(m)
ch := it.SeqToChannel2(1, kv)
for pair := range ch {
// pair.A is key, pair.B is value
}Variant:Similar:Prototypes:func SeqToChannel[T any](bufferSize int, collection iter.Seq[T]) <-chan T
func SeqToChannel2[K, V any](bufferSize int, collection iter.Seq2[K, V]) <-chan Tuple2[K, V]ChannelToSeqโ
Builds an
iter.Seq
from a channel. The returned sequence yields each value received from the channel in order. Iteration blocks until the channel is closed.Examples:
ch := make(chan int, 3)
ch <- 1; ch <- 2; ch <- 3
close(ch)
seq := it.ChannelToSeq(ch)
var got []int
for v := range seq {
got = append(got, v)
}
// got == []int{1, 2, 3}Similar:Prototype:func ChannelToSeq[T any](ch <-chan T) iter.Seq[T]
SeqToChannel2โ
SeqToChannel2 returns a read-only channel of key-value tuple elements from a sequence.
collection := func(yield func(int, string) bool) {
yield(1, "a")
yield(2, "b")
}
ch := it.SeqToChannel2(10, collection)
for tuple := range ch {
fmt.Printf("%d: %s\n", tuple.A, tuple.B)
}
// 1: a
// 2: bChannelToSeq returns a sequence built from channel items. Blocks until channel closes.
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
seq := it.ChannelToSeq(ch)
for item := range seq {
fmt.Println(item)
}
// 1
// 2
// 3Variant:Similar:Prototypes:func SeqToChannel2[K, V any](bufferSize int, collection iter.Seq2[K, V]) <-chan lo.Tuple2[K, V]
func ChannelToSeq[T any](ch <-chan T) iter.Seq[T]