Skip to content

std/io

import "github.com/eugenioenko/goalchemy/std/io"

Package io provides basic interfaces to I/O primitives and helpers that combine them, matching Go’s io package. Pipe is not provided.

const (
SeekStart = 0
SeekCurrent = 1
SeekEnd = 2
)

Seek whence values.

var EOF = errors.New("EOF")

EOF is the error returned by Read when no more input is available.

var ErrClosedPipe = errors.New("io: read/write on closed pipe")

ErrClosedPipe is the error used for read or write operations on a closed pipe.

var ErrNoProgress = errors.New("multiple Read calls return no data or error")

ErrNoProgress is returned when many calls to Read have failed to return any data or error.

var ErrShortBuffer = errors.New("short buffer")

ErrShortBuffer means that a read required a longer buffer than was provided.

var ErrShortWrite = errors.New("short write")

ErrShortWrite means that a write accepted fewer bytes than requested but failed to return an explicit error.

var ErrUnexpectedEOF = errors.New("unexpected EOF")

ErrUnexpectedEOF means that EOF was encountered in the middle of reading a fixed-size block or data structure.

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

Copy copies from src to dst until EOF or an error. A successful Copy returns err == nil, not EOF.

func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)

CopyBuffer is identical to Copy except that it stages through buf. It panics if buf is non-nil and empty.

func CopyN(dst Writer, src Reader, n int64) (written int64, err error)

CopyN copies n bytes, or until an error, from src to dst.

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 EOF.

func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)

ReadAtLeast reads from r into buf until it has read at least min bytes. It returns ErrShortBuffer if min is larger than buf, EOF if no bytes were read, and ErrUnexpectedEOF if EOF happens after reading fewer than min bytes.

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

ReadFull reads exactly len(buf) bytes from r into buf.

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

WriteString writes s to w, using w’s WriteString method when it has one.

type ByteReader interface {
ReadByte() (byte, error)
}

ByteReader wraps the ReadByte method.

type ByteScanner interface {
ByteReader
UnreadByte() error
}

ByteScanner adds the UnreadByte method to ByteReader.

type ByteWriter interface {
WriteByte(c byte) error
}

ByteWriter wraps the WriteByte method.

type Closer interface {
Close() error
}

Closer wraps the basic Close method.

type LimitedReader struct {
R Reader
N int64
}

LimitedReader reads from R but limits the amount of data returned to just N bytes.

func (l *LimitedReader) Read(p []byte) (n int, err error)

Read reads at most the remaining N bytes.

type OffsetWriter struct {
}

OffsetWriter maps writes at offset base to offset base+off in the underlying WriterAt.

func NewOffsetWriter(w WriterAt, off int64) *OffsetWriter

NewOffsetWriter returns an OffsetWriter that writes to w starting at off.

func (o *OffsetWriter) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker relative to the base offset; SeekEnd is not supported.

func (o *OffsetWriter) Write(p []byte) (n int, err error)

Write writes at the current offset and advances it.

func (o *OffsetWriter) WriteAt(p []byte, off int64) (n int, err error)

WriteAt writes at off relative to the base offset.

type ReadCloser interface {
Reader
Closer
}

ReadCloser groups the basic Read and Close methods.

func NopCloser(r Reader) ReadCloser

NopCloser returns a ReadCloser with a no-op Close method wrapping r. If r implements WriterTo, the result does too.

type ReadSeekCloser interface {
Reader
Seeker
Closer
}

ReadSeekCloser groups the basic Read, Seek and Close methods.

type ReadSeeker interface {
Reader
Seeker
}

ReadSeeker groups the basic Read and Seek methods.

type ReadWriteCloser interface {
Reader
Writer
Closer
}

ReadWriteCloser groups the basic Read, Write and Close methods.

type ReadWriteSeeker interface {
Reader
Writer
Seeker
}

ReadWriteSeeker groups the basic Read, Write and Seek methods.

type ReadWriter interface {
Reader
Writer
}

ReadWriter groups the basic Read and Write methods.

type Reader interface {
Read(p []byte) (n int, err error)
}

Reader wraps the basic Read method.

func LimitReader(r Reader, n int64) Reader

LimitReader returns a Reader that reads from r but stops with EOF after n bytes.

func MultiReader(readers ...Reader) Reader

MultiReader returns a Reader that is the logical concatenation of readers.

func TeeReader(r Reader, w Writer) Reader

TeeReader returns a Reader that writes to w what it reads from r. Any error writing to w is returned as a read error.

type ReaderAt interface {
ReadAt(p []byte, off int64) (n int, err error)
}

ReaderAt wraps the basic ReadAt method.

type ReaderFrom interface {
ReadFrom(r Reader) (n int64, err error)
}

ReaderFrom wraps the ReadFrom method.

type RuneReader interface {
ReadRune() (r rune, size int, err error)
}

RuneReader wraps the ReadRune method.

type RuneScanner interface {
RuneReader
UnreadRune() error
}

RuneScanner adds the UnreadRune method to RuneReader.

type SectionReader struct {
}

SectionReader implements Read, Seek and ReadAt on a section of an underlying ReaderAt.

func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader

NewSectionReader returns a SectionReader that reads from r starting at offset off and stops with EOF after n bytes.

func (s *SectionReader) Outer() (r ReaderAt, off int64, n int64)

Outer returns the underlying ReaderAt and offsets of the section.

func (s *SectionReader) Read(p []byte) (n int, err error)

Read implements io.Reader within the section.

func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt relative to the section.

func (s *SectionReader) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker relative to the section.

func (s *SectionReader) Size() int64

Size returns the size of the section in bytes.

type Seeker interface {
Seek(offset int64, whence int) (int64, error)
}

Seeker wraps the basic Seek method.

type StringWriter interface {
WriteString(s string) (n int, err error)
}

StringWriter wraps the WriteString method.

type WriteCloser interface {
Writer
Closer
}

WriteCloser groups the basic Write and Close methods.

type WriteSeeker interface {
Writer
Seeker
}

WriteSeeker groups the basic Write and Seek methods.

type Writer interface {
Write(p []byte) (n int, err error)
}

Writer wraps the basic Write method.

var Discard Writer = discard{}

Discard is a Writer on which all Write calls succeed without doing anything.

func MultiWriter(writers ...Writer) Writer

MultiWriter creates a writer that duplicates its writes to all the provided writers, stopping at the first error.

type WriterAt interface {
WriteAt(p []byte, off int64) (n int, err error)
}

WriterAt wraps the basic WriteAt method.

type WriterTo interface {
WriteTo(w Writer) (n int64, err error)
}

WriterTo wraps the WriteTo method.