Gojo
Esc
  • Loading…
io
Documentation

Standard library

std/io

Errors from the operating system, files, processes and the network, and the interfaces for streams of bytes.

fn copyFile(from: str, to: str) ! io.Error {
    var src = try os.open(from)
    var dst = try os.create(to)
    _ = try io.copy(&dst, &src)
}

Types: enum ErrorKind, struct Error, interface Reader, interface Writer, struct BytesReader, struct Buffer

Functions: fromCode, newError, readAll, readText, copy, writeText, bytesReader, textReader

Types

enum ErrorKind

pub copy enum ErrorKind {
    Other
    NotFound
    PermissionDenied
    AlreadyExists
    NotADirectory
    IsADirectory
    DirectoryNotEmpty
    InvalidInput
    UnexpectedEof
    BrokenPipe
    ConnectionRefused
    ConnectionReset
    AddressInUse
    TimedOut
    Cancelled
    Interrupted
}

What went wrong, independent of the platform.

struct Error

pub struct Error {
    pub kind: ErrorKind
    pub msg: String
    // the operating system's error number, or 0
    pub code: int = 0
}

An error from the operating system or a stream: what kind, and a message that says what was being done (open notes.txt: no such file or directory). os.Error and net.Error are this type.

Error.message

pub fn message(self) -> String

The message, as print shows it.

Error.isCancelled

pub fn isCancelled(self) -> bool

The call stopped because its task was cancelled.

Error.isNotFound

pub fn isNotFound(self) -> bool

The file or directory does not exist.

interface Reader

pub interface Reader {
    // Appends at most `max` bytes to `buf` and returns how many; 0 means the
    // end of the stream. Waits until at least one byte is there.
    fn read(inout self, buf: inout Vec[byte], max: int) -> int ! Error
}

A source of bytes: files, connections, process output, io.BytesReader.

interface Writer

pub interface Writer {
    // Writes all of `data`.
    fn write(inout self, data: []byte) ! Error
}

A sink for bytes: files, connections, io.Buffer.

struct BytesReader

pub struct BytesReader {
    // and private fields
}

Reads from bytes in memory.

BytesReader.read

pub fn read(inout self, buf: inout Vec[byte], max: int) -> int ! Error

Appends up to max of the remaining bytes to buf.

struct Buffer

pub struct Buffer {
    pub data: Vec[byte] = []
}

Collects what is written to it.

Buffer.write

pub fn write(inout self, data: []byte) ! Error

Appends data; never fails.

Buffer.text

pub fn text(self) -> String

What was written, as a string.

Functions

fromCode

pub fn fromCode(what: str, code: int) -> Error

An error from the operating system's error number code, for what ("open data.txt"). Used by os, net and code calling C directly.

newError

pub fn newError(kind: ErrorKind, msg: str) -> Error

An error that doesn't come from the operating system.

readAll

pub fn readAll(r: inout Reader) -> Vec[byte] ! Error

Reads r to its end.

readText

pub fn readText(r: inout Reader) -> String ! Error

Reads r to its end as text.

copy

pub fn copy(dst: inout Writer, src: inout Reader) -> int ! Error

Copies src into dst until src ends; returns the number of bytes.

writeText

pub fn writeText(w: inout Writer, s: str) ! Error

Writes the bytes of s to w.

bytesReader

pub fn bytesReader(data: sink Vec[byte]) -> BytesReader

A reader of data.

textReader

pub fn textReader(s: str) -> BytesReader

A reader of the bytes of s.