initial commit
This commit is contained in:
32
chan_io.go
Normal file
32
chan_io.go
Normal file
@ -0,0 +1,32 @@
|
||||
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)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user