io

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2021 License: MIT Imports: 3 Imported by: 1

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

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingWriter   = errors.New("missing writer")
	ErrMissingReader   = errors.New("missing reader")
	ErrMissingResource = errors.New("missing resource")
)
View Source
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

func CloseReaderAndWriter(inputReader Reader, outputWriter Writer, brokenPipe bool) (error, error)

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

func Copy(dst Writer, src Reader) (int64, error)

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

func CopyFlushClose(w Writer, r Reader) error

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 FlushClose

func FlushClose(w Writer) error

FlushClose flushes and then closes the writer.

func ReadAll

func ReadAll(r Reader) ([]byte, error)

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

func ReadAllAndClose(r Reader) ([]byte, error)

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

func ReadFull(r Reader, buf []byte) (int, error)

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

func WriteError(w Writer, err error) (int, error)

WriteError writes an error with a trailing newline to the writer and returns an error, if any.

func WriteLine

func WriteLine(w Writer, s string) (int, error)

WriteLine writes a string with a trailing newline to the writer and returns an error, if any.

func WriteString

func WriteString(w Writer, s string) (int, error)

WriteString writes a string 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 Closer

type Closer interface {
	io.Closer
}

Closer is a copy of the standard library io.Closer interface.

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 Reader

type Reader interface {
	io.Reader
}

Reader is a copy of the standard library io.Reader interface.

type ReaderAt

type ReaderAt interface {
	io.ReaderAt
}

ReaderAt is a copy of the standard library io.ReaderAt interface.

type WriteCloser

type WriteCloser interface {
	Writer
	io.Closer
}

WriteCloser is an interface that extends io.Writer and io.Closer.

type Writer

type Writer interface {
	io.Writer
}

Writer is a copy of the standard library io.Writer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL