Documentation
¶
Overview ¶
Package io supplements the interfaces provided by the standard library io package and provides functions for using those interfaces. The interfaces and functions in this package are used throughout the grw package.
Index ¶
- Variables
- func Close(i interface{}) error
- func CloseReaderAndWriter(inputReader Reader, outputWriter Writer, brokenPipe bool) (error, error)
- func Copy(dst Writer, src Reader) (int64, error)
- func CopyFlushClose(w Writer, r Reader) error
- func Flush(w interface{}) error
- func FlushClose(w Writer) error
- func ReadAll(r Reader) ([]byte, error)
- func ReadAllAndClose(r Reader) ([]byte, error)
- func ReadFull(r Reader, buf []byte) (int, error)
- func ReadString(r ByteReader, delim byte) (string, error)
- func WriteError(w Writer, err error) (int, error)
- func WriteLine(w Writer, s string) (int, error)
- func WriteString(w Writer, s string) (int, error)
- type Buffer
- type ByteReadCloser
- type ByteReader
- type ByteWriteCloser
- type ByteWriter
- type Closer
- type Flusher
- type ReadCloser
- type Reader
- type ReaderAt
- type WriteCloser
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingWriter = errors.New("missing writer") ErrMissingReader = errors.New("missing reader") ErrMissingResource = errors.New("missing resource") )
var (
EOF = io.EOF
)
Functions ¶
func Close ¶
func Close(i interface{}) error
Close closes the given resource if it has a Close method. If the given resource does not have a Close method, then it simply returns nil. If the given resource is nil, then returns the ErrMissingResource error.
func CloseReaderAndWriter ¶
CloseReaderAndWriter closes an input reader and output writer, and does not flush the writer if the pipe is broken. CloseReaderAndWriter will close the writer even if closing the reader returned an error.
The returns values are the errors returned when closing the reader and closing the writer, respectively.
func Copy ¶
Copy copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and the first error encountered while copying, if any.
A successful Copy returns err == nil, not err == EOF. Because Copy is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported.
If src implements the WriterTo interface, the copy is implemented by calling src.WriteTo(dst). Otherwise, if dst implements the ReaderFrom interface, the copy is implemented by calling dst.ReadFrom(src).
func CopyFlushClose ¶
CopyFlushClose copies all data from the reader to the writer, flushes the writer, and then closes the reader and writer. If there is an error closing the reader, will still attempt to close the writer.
func Flush ¶
func Flush(w interface{}) error
Flush flushes the given writer if it has a Flush method. If the given writer does not have a Flush method, then it simply returns nil. If the given writer is nil, then returns the ErrMissingWriter error.
func ReadAll ¶
ReadAll reads from r until an error or EOF and returns the data it read. A successful call returns err == nil, not err == EOF. Because ReadAll is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported. If the given reader is nil, returns ErrMissingReader error.
func ReadAllAndClose ¶
ReadAllAndClose reads all the data from the reader and calls its close method (if it has one). ReadAllAndClose will attempt to close the reader even if there was an error during reading.
func ReadFull ¶
ReadFull reads exactly len(buf) bytes from r into buf. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read. If an EOF happens after reading some but not all the bytes, ReadFull returns ErrUnexpectedEOF. On return, n == len(buf) if and only if err == nil. If r returns an error having read at least len(buf) bytes, the error is dropped.
func ReadString ¶
func ReadString(r ByteReader, delim byte) (string, error)
ReadString returns a string of all the bytes up to and including the first occurrence of the given delimiter and an error, if any. If the given reader is nil, returns ErrMissingReader error.
func WriteError ¶
WriteError writes an error with a trailing newline to the writer and returns an error, if any.
Types ¶
type Buffer ¶
type Buffer interface { io.Reader Writer io.ByteWriter WriteRune(r rune) (n int, err error) WriteString(s string) (n int, err error) Bytes() []byte String() string Len() int }
Buffer is an interface that supports common buffer methods, including those from the bytes.Buffer struct. Buffer extends io.Reader, io.Writer, and io.ByteWriter interfaces. It supports other buffer implementations, too.
type ByteReadCloser ¶
type ByteReadCloser interface { ByteReader io.Closer io.ReaderAt ReadAll() ([]byte, error) ReadAllAndClose() ([]byte, error) ReadFirst() (byte, error) ReadRange(start int, end int) ([]byte, error) }
ByteReadCloser extends ByteReader, io.Closer, and io.ReaderAt interfaces, and provides functions for reading a range of bytes.
type ByteReader ¶
type ByteReader interface { io.Reader io.ByteReader ReadBytes(delim byte) ([]byte, error) ReadString(delim byte) (string, error) }
ByteReader is an interface that supports reading bytes. Buffer extends io.Reader io.ByteReader interfaces.
type ByteWriteCloser ¶
type ByteWriteCloser interface { ByteWriter Flusher Closer Lock() // lock the writer for exclusive use Unlock() // unlock the writer FlushSafe() error // lock the writer before flushing (to prvent another routine from writing concurrently). CloseSafe() error // lock the writer before closing (to prevent another routine from writing concurrently) WriteString(s string) (n int, err error) WriteLine(s string) (n int, err error) WriteLineSafe(s string) (n int, err error) WriteError(e error) (n int, err error) WriteErrorSafe(e error) (n int, err error) }
ByteWriteCloser is an interface that supports writing bytes. ByteWriteCloser extends ByteWriter, Flusher, and io.Closer interfaces. ByteWriterCloser provides a Lock and Unlock function for interacting with a mutex to lock the writer for exclusive use and prevent concurrency errors.
type ByteWriter ¶
type ByteWriter interface { Writer io.ByteWriter }
ByteWriter is an interface that extends io.Writer and io.ByteWriter. ByteWriter provides functions for writing bytes.
type Flusher ¶
type Flusher interface {
Flush() error
}
Flusher is a simple interface that provides the common Flush function used by buffered writers.
type ReadCloser ¶
type ReadCloser interface { io.ReadCloser }
ReadCloser is a copy of the standard library io.ReadCloser interface.
type WriteCloser ¶
WriteCloser is an interface that extends io.Writer and io.Closer.
Source Files
¶
- Buffer.go
- ByteReadCloser.go
- ByteReader.go
- ByteWriteCloser.go
- ByteWriter.go
- Close.go
- CloseReaderAndWriter.go
- Closer.go
- Copy.go
- CopyFlushClose.go
- Flush.go
- FlushClose.go
- Flusher.go
- ReadAll.go
- ReadAllAndClose.go
- ReadCloser.go
- ReadFull.go
- ReadString.go
- Reader.go
- ReaderAt.go
- WriteCloser.go
- WriteError.go
- WriteLine.go
- WriteString.go
- Writer.go
- io.go