std / io

io

import "std:io"

Provides fundamental I/O abstractions for reading and writing data. Defines generic Reader, Writer, Closer, and composite types (ReadWriter, ReadCloser, WriteCloser, ReadWriteCloser) for callback-based I/O.

View source on Codeberg →

Types

#
type Error int

Represents an I/O error code.

#
type Reader struct { /* unexported fields */ }

Provides generic reading via callback.

#
type Writer struct { /* unexported fields */ }

Provides generic writing via callback.

#
type Closer struct { /* unexported fields */ }

Provides generic closing via callback.

#
type ReadWriter struct { /* unexported fields */ }

Combines reading and writing via callbacks.

#
type ReadCloser struct { /* unexported fields */ }

Combines reading and closing via callbacks.

#
type WriteCloser struct { /* unexported fields */ }

Combines writing and closing via callbacks.

#
type ReadWriteCloser struct { /* unexported fields */ }

Combines reading, writing, and closing via callbacks.

Error Constants

#
const (
    OK          Error = iota
    EOF
    ErrClosed
    ErrInvalid
    ErrIO
    ErrNoMem
    ErrPerm
    ErrExist
    ErrNotExist
)

Error constants: OK indicates success, EOF indicates end of stream, ErrClosed indicates the stream is closed, ErrInvalid indicates an invalid argument, ErrIO indicates an underlying I/O error, ErrNoMem indicates out of memory, ErrPerm indicates a permission error, ErrExist indicates the file already exists, and ErrNotExist indicates the file does not exist.

Generic Reader/Writer

#
func NewReader(source any, readFn func(any, []byte) (int, Error)) Reader

Creates a Reader with the given source and read callback.

#
func NewWriter(dest any, writeFn func(any, []byte) (int, Error)) Writer

Creates a Writer with the given destination and write callback.

#
func Read(r Reader, buf []byte) (int, Error)

Reads up to len(buf) bytes from the Reader into buf.

#
func Write(w Writer, buf []byte) (int, Error)

Writes buf to the Writer.

#
func ReadAll(r Reader) (list[byte], Error)

Reads from the Reader until EOF. Caller must free the result.

#
func WriteAll(w Writer, buf []byte) Error

Writes all bytes from buf to the Writer.

Composite Constructors

#
func NewCloser(target any, closeFn func(any) Error) Closer

Creates a Closer with the given target and close callback.

#
func NewReadWriter(target any, readFn func(any, []byte) (int, Error), writeFn func(any, []byte) (int, Error)) ReadWriter

Creates a ReadWriter with the given target, read callback, and write callback.

#
func NewReadCloser(target any, readFn func(any, []byte) (int, Error), closeFn func(any) Error) ReadCloser

Creates a ReadCloser with the given target, read callback, and close callback.

#
func NewWriteCloser(target any, writeFn func(any, []byte) (int, Error), closeFn func(any) Error) WriteCloser

Creates a WriteCloser with the given target, write callback, and close callback.

#
func NewReadWriteCloser(target any, readFn func(any, []byte) (int, Error), writeFn func(any, []byte) (int, Error), closeFn func(any) Error) ReadWriteCloser

Creates a ReadWriteCloser with the given target, read callback, write callback, and close callback.

Composite Operations

#
func CloseCloser(c Closer) Error

Closes the Closer by invoking its close callback.

#
func ReadRW(rw ReadWriter, buf []byte) (int, Error)

Reads from a ReadWriter into buf.

#
func WriteRW(rw ReadWriter, buf []byte) (int, Error)

Writes buf to a ReadWriter.

#
func ReadRC(rc ReadCloser, buf []byte) (int, Error)

Reads from a ReadCloser into buf.

#
func CloseRC(rc ReadCloser) Error

Closes a ReadCloser.

#
func WriteWC(wc WriteCloser, buf []byte) (int, Error)

Writes buf to a WriteCloser.

#
func CloseWC(wc WriteCloser) Error

Closes a WriteCloser.

#
func ReadRWC(rwc ReadWriteCloser, buf []byte) (int, Error)

Reads from a ReadWriteCloser into buf.

#
func WriteRWC(rwc ReadWriteCloser, buf []byte) (int, Error)

Writes buf to a ReadWriteCloser.

#
func CloseRWC(rwc ReadWriteCloser) Error

Closes a ReadWriteCloser.

Extractors

#
func ReaderFromRW(rw ReadWriter) Reader

Extracts a Reader from a ReadWriter.

#
func WriterFromRW(rw ReadWriter) Writer

Extracts a Writer from a ReadWriter.

#
func ReaderFromRC(rc ReadCloser) Reader

Extracts a Reader from a ReadCloser.

#
func WriterFromWC(wc WriteCloser) Writer

Extracts a Writer from a WriteCloser.

#
func ReaderFromRWC(rwc ReadWriteCloser) Reader

Extracts a Reader from a ReadWriteCloser.

#
func WriterFromRWC(rwc ReadWriteCloser) Writer

Extracts a Writer from a ReadWriteCloser.

Utilities

#
func Copy(dst Writer, src Reader) (int, Error)

Copies from Reader to Writer until EOF.

#
func NopCloser(r Reader) ReadCloser

Wraps a Reader with a no-op Close.