All packages

io

import "io"

Package io provides basic interfaces to I/O primitives. Its primary job is to wrap existing implementations of such primitives, such as those in package os, into shared public interfaces that abstract the functionality, plus some other related primitives. Because these interfaces and primitives wrap lower-level operations with various implementations, unless otherwise informed clients should not assume they are safe for parallel execution.

Go 1.26.1

Constants

const (
	SeekStart   = 0 // seek relative to the origin of the file
	SeekCurrent = 1 // seek relative to the current offset
	SeekEnd     = 2 // seek relative to the end
)

Seek whence values.

io/io.go:23

const (
	SeekStart   = 0 // seek relative to the origin of the file
	SeekCurrent = 1 // seek relative to the current offset
	SeekEnd     = 2 // seek relative to the end
)

Seek whence values.

io/io.go:24

const (
	SeekStart   = 0 // seek relative to the origin of the file
	SeekCurrent = 1 // seek relative to the current offset
	SeekEnd     = 2 // seek relative to the end
)

Seek whence values.

io/io.go:22

Variables

var EOF = errors.New("EOF")

EOF is the error returned by Read when no more input is available. (Read must return EOF itself, not an error wrapping EOF, because callers will test for EOF using ==.) Functions should return EOF only to signal a graceful end of input. If the EOF occurs unexpectedly in a structured data stream, the appropriate error is either [ErrUnexpectedEOF] or some other error giving more detail.

io/io.go:44

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

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

io/pipe.go:36

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

ErrNoProgress is returned by some clients of a [Reader] when many calls to Read have failed to return any data or error, usually the sign of a broken [Reader] implementation.

io/io.go:53

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

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

io/io.go:35

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

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

io/io.go:29

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

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

io/io.go:48

Functions

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 [WriterTo], the copy is implemented by calling src.WriteTo(dst). Otherwise, if dst implements [ReaderFrom], the copy is implemented ...

io/io.go:387

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

CopyBuffer is identical to Copy except that it stages through the provided buffer (if one is required) rather than allocating a temporary one. If buf is nil, one is allocated; otherwise if it has zero length, CopyBuffer panics. If either src implements [WriterTo] or dst implements [ReaderFrom], buf will not be used to perform the copy.

io/io.go:398

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

CopyN copies n bytes (or until an error) from src to dst. It returns the number of bytes copied and the earliest error encountered while copying. On return, written == n if and only if err == nil. If dst implements [ReaderFrom], the copy is implemented using it.

io/io.go:363

func LimitReader(r Reader, n int64) Reader

LimitReader returns a Reader that reads from r but stops with EOF after n bytes. The underlying implementation is a *LimitedReader.

io/io.go:461

func MultiReader(readers ...Reader) Reader

MultiReader returns a Reader that's the logical concatenation of the provided input readers. They're read sequentially. Once all inputs have returned EOF, Read will return EOF. If any of the readers return a non-nil, non-EOF error, Read will return that error.

io/multi.go:73

func MultiWriter(writers ...Writer) Writer

MultiWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command. Each write is written to each listed writer, one at a time. If a listed writer returns an error, that overall write operation stops and returns the error; it does not continue down the list.

io/multi.go:127

func NewOffsetWriter(w WriterAt, off int64) *OffsetWriter

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

io/io.go:578

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.

io/io.go:486

func NopCloser(r Reader) ReadCloser

NopCloser returns a [ReadCloser] with a no-op Close method wrapping the provided [Reader] r. If r implements [WriterTo], the returned [ReadCloser] will implement [WriterTo] by forwarding calls to r.

io/io.go:682

func Pipe() (*PipeReader, *PipeWriter)

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

io/pipe.go:195

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.

io/io.go:709

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

ReadAtLeast reads from r into buf until it has read at least min bytes. 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 fewer than min bytes, ReadAtLeast returns [ErrUnexpectedEOF]. If min is greater than the length of buf, ReadAtLeast returns [ErrShortBuffer]. On return, n >= min if and only if err == nil. If r returns an error having read at least min bytes, the error is dropped.

io/io.go:329

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.

io/io.go:353

func TeeReader(r Reader, w Writer) Reader

TeeReader returns a [Reader] that writes to w what it reads from r. All reads from r performed through it are matched with corresponding writes to w. There is no internal buffering - the write must complete before the read completes. Any error encountered while writing is reported as a read error.

io/io.go:618

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

WriteString writes the contents of the string s to w, which accepts a slice of bytes. If w implements [StringWriter], [StringWriter.WriteString] is invoked directly. Otherwise, [Writer.Write] is called exactly once.

io/io.go:314

Types

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

ByteReader is the interface that wraps the ReadByte method. ReadByte reads and returns the next byte from the input or any error encountered. If ReadByte returns an error, no input byte was consumed, and the returned byte value is undefined. ReadByte provides an efficient interface for byte-at-time processing. A [Reader] that does not implement ByteReader can be wrapped using bufio.NewReader to add this method.

io/io.go:262

type ByteScanner interface {
	ByteReader
	UnreadByte() error
}

ByteScanner is the interface that adds the UnreadByte method to the basic ReadByte method. UnreadByte causes the next call to ReadByte to return the last byte read. If the last operation was not a successful call to ReadByte, UnreadByte may return an error, unread the last byte read (or the byte prior to the last-unread byte), or (in implementations that support the [Seeker] interface) seek to one byte before the current offset.

io/io.go:274

type ByteWriter interface {
	WriteByte(c byte) error
}

ByteWriter is the interface that wraps the WriteByte method.

io/io.go:280

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.

io/io.go:107

type LimitedReader struct {
	R Reader // underlying reader
	N int64  // max bytes remaining
}

A LimitedReader reads from R but limits the amount of data returned to just N bytes. Each call to Read updates N to reflect the new amount remaining. Read returns EOF when N <= 0 or when the underlying R returns EOF.

io/io.go:467

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

io/io.go:472

type OffsetWriter struct {
	// contains filtered or unexported fields
}

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

io/io.go:570

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

io/io.go:597

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

io/io.go:582

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

io/io.go:588

type PipeReader struct {
	// contains filtered or unexported fields
}

A PipeReader is the read half of a pipe.

io/pipe.go:126

func (r *PipeReader) Close() error

Close closes the reader; subsequent writes to the write half of the pipe will return the error [ErrClosedPipe].

io/pipe.go:139

func (r *PipeReader) 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.

io/pipe.go:148

func (r *PipeReader) Read(data []byte) (int, 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.

io/pipe.go:133

type PipeWriter struct {
	// contains filtered or unexported fields
}

A PipeWriter is the write half of a pipe.

io/pipe.go:153

func (w *PipeWriter) Close() error

Close closes the writer; subsequent reads from the read half of the pipe will return no bytes and EOF.

io/pipe.go:166

func (w *PipeWriter) 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.

io/pipe.go:176

func (w *PipeWriter) Write(data []byte) (int, 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].

io/pipe.go:160

type ReadCloser interface {
	Reader
	Closer
}

ReadCloser is the interface that groups the basic Read and Close methods.

io/io.go:137

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

Reader is the interface that wraps the basic Read method. Read reads up to len(p) bytes into p. It returns the number of bytes 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 bytes, it returns t...

io/io.go:86

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

ReaderAt is the interface that wraps the basic ReadAt method. ReadAt reads len(p) bytes into p starting at offset off in the underlying input source. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. When ReadAt returns n < len(p), it returns a non-nil error explaining why more bytes were not returned. In this respect, ReadAt is stricter than Read. Even if ReadAt returns n < len(p), it may use all of p as scratch space during the call. If some data is availa...

io/io.go:230

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

ReaderFrom is the interface that wraps the ReadFrom method. ReadFrom reads data from r until EOF or error. The return value n is the number of bytes read. Any error except EOF encountered during the read is also returned. The [Copy] function uses [ReaderFrom] if available.

io/io.go:189

type ReadSeekCloser interface {
	Reader
	Seeker
	Closer
}

ReadSeekCloser is the interface that groups the basic Read, Seek and Close methods.

io/io.go:163

type ReadSeeker interface {
	Reader
	Seeker
}

ReadSeeker is the interface that groups the basic Read and Seek methods.

io/io.go:156

type ReadWriteCloser interface {
	Reader
	Writer
	Closer
}

ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.

io/io.go:149

type ReadWriter interface {
	Reader
	Writer
}

ReadWriter is the interface that groups the basic Read and Write methods.

io/io.go:131

type ReadWriteSeeker interface {
	Reader
	Writer
	Seeker
}

ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.

io/io.go:176

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

RuneReader is the interface that wraps the ReadRune method. ReadRune reads a single encoded Unicode character and returns the rune and its size in bytes. If no character is available, err will be set.

io/io.go:289

type RuneScanner interface {
	RuneReader
	UnreadRune() error
}

RuneScanner is the interface that adds the UnreadRune method to the basic ReadRune method. UnreadRune causes the next call to ReadRune to return the last rune read. If the last operation was not a successful call to ReadRune, UnreadRune may return an error, unread the last rune read (or the rune prior to the last-unread rune), or (in implementations that support the [Seeker] interface) seek to the start of the rune before the current offset.

io/io.go:301

type SectionReader struct {
	// contains filtered or unexported fields
}

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

io/io.go:501

func (s *SectionReader) Outer() (ReaderAt, int64, int64)

Outer returns the underlying [ReaderAt] and offsets for the section. The returned values are the same that were passed to [NewSectionReader] when the [SectionReader] was created.

io/io.go:565

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

io/io.go:509

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

io/io.go:542

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

io/io.go:524

func (s *SectionReader) Size() int64

Size returns the size of the section in bytes.

io/io.go:559

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

Seeker is the interface that wraps the basic Seek method. Seek sets the offset for the next Read or Write to offset, interpreted according to whence: [SeekStart] means relative to the start of the file, [SeekCurrent] means relative to the current offset, and [SeekEnd] means relative to the end (for example, offset = -2 specifies the penultimate byte of the file). Seek returns the new offset relative to the start of the file or an error, if any. Seeking to an offset before the start of the f...

io/io.go:126

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

StringWriter is the interface that wraps the WriteString method.

io/io.go:307

type WriteCloser interface {
	Writer
	Closer
}

WriteCloser is the interface that groups the basic Write and Close methods.

io/io.go:143

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

Writer is the interface that wraps the basic Write method. Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes 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.

io/io.go:99

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

WriterAt is the interface that wraps the basic WriteAt method. WriteAt writes len(p) bytes from p to the underlying data stream at offset off. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. WriteAt must return a non-nil error if it returns n < len(p). If WriteAt is writing to a destination with a seek offset, WriteAt should not affect nor be affected by the underlying seek offset. Clients of WriteAt can execut...

io/io.go:249

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

WriterTo is the interface that wraps the WriteTo method. WriteTo writes data to w until there's no more data to write or when an error occurs. The return value n is the number of bytes written. Any error encountered during the write is also returned. The Copy function uses WriterTo if available.

io/io.go:200

type WriteSeeker interface {
	Writer
	Seeker
}

WriteSeeker is the interface that groups the basic Write and Seek methods.

io/io.go:170