Generic extension of Go's io pkg.
Index
Expand/collapse section
This package does not define any new errors, it inherits them from the io
package in the standard library.
io.EOF // Used by e.g iox.Reader: Stop reading/consuming
io.ErrClosedPipe // Used by e.g iox.Writer: Stop writing/producing.
Core interfaces are the iox.Reader
and iox.Writer
listed below. They mirror io.Reader
and io.Writer
but differ in that they work with generic values instead. An extra addition is the use of context.Context
, since io often involves program bounds (also it gives some added flexibility).
type Reader[T any] interface {
Read(context.Context) (T, error)
}
type Writer[T any] interface {
Write(context.Context, T) error
}
As with the io package from the standard library, iox readers and writers are combined with eachother and io.Closer. The full set of combinations can be seen by clicking here
type Reader[T any] interface {
Read(context.Context) (T, error)
}
type ReadCloser[T any] interface {
io.Closer
Reader[T]
}
type Writer[T any] interface {
Write(context.Context, T) error
}
type WriteCloser[T any] interface {
io.Closer
Writer[T]
}
type ReadWriter[T, U any] interface {
Reader[T]
Writer[U]
}
type ReadWriteCloser[T, U any] interface {
io.Closer
Reader[T]
Writer[U]
}
There are also "impl" structs which let you implement core interfaces with functions, which reduces a lot of boilerplate. These can be seen by clicking on this section
Signatures are links to the Go playground (examples).
All links go to examples on the Go playground.
func NewReaderFrom[T any](vs ...T) Reader[T]
func NewReaderFromBytes[T any](r io.Reader) func(f decoderFn) Reader[T]
func NewReaderFromValues[T any](r Reader[T]) func(f encoderFn) io.Reader
func NewWriterFromValues[T any](w io.Writer) func(f encoderFn) Writer[T]
func NewWriterFromBytes[T any](w Writer[T]) func(f decoderFn) io.Writer
func NewReadWriterFrom[T any](vs ...T) ReadWriter[T, T]
Alternatively, you may see signatures and docs by clicking here
// NewReaderFrom returns a Reader which yields values from the given vals.
func NewReaderFrom[T any](vs ...T) Reader[T]
// NewReaderFromBytes creates a new T reader from an io.Reader and Decoder.
// It simply reads bytes from 'r', decodes them, and passes them along to the
// caller. As such, the decoder must match the encoder used to create the bytes.
// If 'r' is nil, an empty Reader is returned; if 'f' is nil, the decoder is set
// to json.NewDecoder.
func NewReaderFromBytes[T any](r io.Reader) func(f decoderFn) Reader[T]
// NewReaderFromValues creates an io.Reader from a Reader and Encoder.
// It simply reads values from 'r', encodes them, and passes them along to the
// caller. As such, when decoding values from the returned io.Reader one should
// use a decoder which matches the encoder passed here. If 'r' is nil, an
// empty (not nil) io.Reader is returned; if 'f' is nil, the encoder is set to
// json.NewEncoder.
func NewReaderFromValues[T any](r Reader[T]) func(f encoderFn) io.Reader
// NewWriterFromValues returns a Writer which accepts values, encodes them
// using the given encoder, and then writes them to 'w'. If 'w' is nil, an empty
// Writer is returned; if 'f' is nil, the encoder is set to json.NewEncoder.
func NewWriterFromValues[T any](w io.Writer) func(f encoderFn) Writer[T]
// NewWriterFromBytes returns an io.Writer which accepts bytes, decodes them
// using the given decoder, and then writes them to 'w'. If 'w' is nil, an emtpy
// io.Writer is returned; if 'f' is nil, the decoder is set to json.NewDecoder.
func NewWriterFromBytes[T any](w Writer[T]) func(f decoderFn) io.Writer
// NewReadWriterFrom returns a ReadWriter[T] which writes into- and read from
// an internal buffer. The buffer is initially populated with the given values.
// The buffer acts like a stack, and a read while the buf is empty returns io.EOF.
func NewReadWriterFrom[T any](vs ...T) ReadWriter[T, T]
All links go to examples on the Go playground.
Batching.
func NewReaderWithBatching[T any](r Reader[T], size int) Reader[[]T]
func NewReaderWithUnbatching[T any](r Reader[[]T]) Reader[T]
func NewWriterWithBatching[T any](w Writer[[]T], size int) Writer[T]
func NewWriterWithUnbatching[T any](w Writer[T]) Writer[[]T]
Filtering & mapping.
func NewReaderWithFilterFn[T any](r Reader[T]) func(f func(v T) bool) Reader[T]
func NewReaderWithMapperFn[T, U any](r Reader[T]) func(f func(T) U) Reader[U]
func NewWriterWithFilterFn[T any](w Writer[T]) func(f func(T) bool) Writer[T]
func NewWriterWithMapperFn[T, U any](w Writer[U]) func(f func(T) U) Writer[T]