channel/chan_io.go

29 lines
810 B
Go

package channel
import (
"fmt"
"io"
"time"
)
// WriteInto writes all given values into the channel ch.
// Is is a shorthand for Forward(ch, AsChan(values...))
func WriteInto[T any](ch chan<- T, values ...T) {
Forward(ch, Of(values...))
}
// WriteIntoDelayed writes all given values into the channel ch.
// It sleeps after every write for the given amount of time.
// It is a shorthand for Forward(ch, AsChanDelayed(time, values...))
func WriteIntoDelayed[T any](ch chan<- T, delay time.Duration, values ...T) {
Forward(ch, OfDelayed(delay, values...))
}
// WriteIntoWriter reads all values from ch and writes them via fmt.Fprintln to all writers
func WriteIntoWriter[T any](ch <-chan T, writers ...io.Writer) {
w := io.MultiWriter(writers...)
Each(ch, func(value T) {
fmt.Fprintln(w, value)
})
}