Documentation
¶
Overview ¶
Package gio - generic io - is forked variant of Go stdlib io module. Code is extended to be able to work on a slice of Go any type.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Pipe ¶
func Pipe[T any]() (*PipeReader[T], *PipeWriter[T])
Pipe creates a synchronous in-memory pipe. It can be used to connect code expecting an io.Reader with code expecting an io.Writer.
Reads and Writes on the pipe are matched one to one except when multiple Reads are needed to consume a single Write. That is, each Write to the PipeWriter blocks until it has satisfied one or more Reads from the PipeReader that fully consume the written data. The data is copied directly from the Write to the corresponding Read (or Reads); there is no internal buffering.
It is safe to call Read and Write in parallel with each other or with Close. Parallel calls to Read and parallel calls to Write are also safe: the individual calls will be gated sequentially.
Types ¶
type Closer ¶
type Closer interface {
Close() error
}
Closer is the interface that wraps the basic Close method.
The behavior of Close after the first call is undefined. Specific implementations may document their own behavior.
type PipeReader ¶
type PipeReader[T any] struct { // contains filtered or unexported fields }
A PipeReader is the read half of a pipe.
func (*PipeReader[T]) Close ¶
func (r *PipeReader[T]) Close() error
Close closes the reader; subsequent writes to the write half of the pipe will return the error ErrClosedPipe.
func (*PipeReader[T]) CloseWithError ¶
func (r *PipeReader[T]) CloseWithError(err error) error
CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error err.
CloseWithError never overwrites the previous error if it exists and always returns nil.
func (*PipeReader[T]) Read ¶
func (r *PipeReader[T]) Read(data []T) (n int, err error)
Read implements the standard Read interface: it reads data from the pipe, blocking until a writer arrives or the write end is closed. If the write end is closed with an error, that error is returned as err; otherwise err is EOF.
type PipeWriter ¶
type PipeWriter[T any] struct { // contains filtered or unexported fields }
A PipeWriter is the write half of a pipe.
func (*PipeWriter[T]) Close ¶
func (w *PipeWriter[T]) Close() error
Close closes the writer; subsequent reads from the read half of the pipe will return no bytes and EOF.
func (*PipeWriter[T]) CloseWithError ¶
func (w *PipeWriter[T]) CloseWithError(err error) error
CloseWithError closes the writer; subsequent reads from the read half of the pipe will return no bytes and the error err, or EOF if err is nil.
CloseWithError never overwrites the previous error if it exists and always returns nil.
func (*PipeWriter[T]) Write ¶
func (w *PipeWriter[T]) Write(data []T) (n int, err error)
Write implements the standard Write interface: it writes data to the pipe, blocking until one or more readers have consumed all the data or the read end is closed. If the read end is closed with an error, that err is returned as err; otherwise err is ErrClosedPipe.
type ReadCloser ¶
ReadCloser is the interface that groups the basic Read and Close methods.
type Reader ¶
Reader is the interface that wraps the basic Read method.
Read reads up to len(p) types into p. It returns the number of types read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.
When Read encounters an error or end-of-file condition after successfully reading n > 0 types, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of types at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.
Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some types and also both of the allowed EOF behaviors.
Implementations of Read are discouraged from returning a zero byte count with a nil error, except when len(p) == 0. Callers should treat a return of 0 and nil as indicating that nothing happened; in particular it does not indicate EOF.
Implementations must not retain p.
type WriteCloser ¶
WriteCloser is the interface that groups the basic Write and Close methods.
type Writer ¶
Writer is the interface that wraps the basic Write method.
Write writes len(p) types from p to the underlying data stream. It returns the number of types written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-nil error if it returns n < len(p). Write must not modify the slice data, even temporarily.
Implementations must not retain p.
Directories
¶
Path | Synopsis |
---|---|
package pipe implements type-safe unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go native types which can be connected together like unix allows.
|
package pipe implements type-safe unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go native types which can be connected together like unix allows. |
package pipe implements unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go stream of bytes which can be connected together like unix allows.
|
package pipe implements unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go stream of bytes which can be connected together like unix allows. |