channel/chan_io.go

33 lines
907 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...)
EachSuccessive(ch, func(value T) {
if err, ok := any(value).(error); ok {
fmt.Fprintln(w, err.Error())
return
}
fmt.Fprintln(w, value)
})
}