channel/chan_io.go

30 lines
818 B
Go
Raw Normal View History

2022-02-09 12:10:02 +01:00
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, AsChan(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, AsChanDelayed(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) {
for value := range ch {
for _, w := range writers {
fmt.Fprintln(w, value)
}
}
}