Go Home Page
The Go Programming Language

Package io

import "io"

This package 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.

Package files

io.go pipe.go

Variables

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

var ErrShortWrite os.Error = &Error{"short write"}

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

var ErrUnexpectedEOF os.Error = &Error{"unexpected EOF"}

func Copy

func Copy(dst Writer, src Reader) (written int64, err os.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 error, if any.

If dst implements the ReaderFrom interface, the copy is implemented by calling dst.ReadFrom(src). Otherwise, if src implements the WriterTo interface, the copy is implemented by calling src.WriteTo(dst).

func Copyn

func Copyn(dst Writer, src Reader, n int64) (written int64, err os.Error)

Copyn copies n bytes (or until an error) from src to dst. It returns the number of bytes copied and the error, if any.

If dst implements the ReaderFrom interface, the copy is implemented by calling dst.ReadFrom(src).

func ReadAtLeast

func ReadAtLeast(r Reader, buf []byte, min int) (n int, err os.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 os.EOF only if no bytes were read. If an EOF happens after reading fewer than min bytes, ReadAtLeast returns ErrUnexpectedEOF.

func ReadFull

func ReadFull(r Reader, buf []byte) (n int, err os.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 os.EOF only if no bytes were read. If an EOF happens after reading some but not all the bytes, ReadFull returns ErrUnexpectedEOF.

func WriteString

func WriteString(w Writer, s string) (n int, err os.Error)

WriteString writes the contents of the string s to w, which accepts an array of bytes.

type Closer

Closer is the interface that wraps the basic Close method.

type Closer interface {
    Close() os.Error
}

type Error

Error represents an unexpected I/O behavior.

type Error struct {
    os.ErrorString
}

type PipeReader

A PipeReader is the read half of a pipe.

type PipeReader struct {
    // contains unexported fields
}

func Pipe

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 on one end are matched with writes on the other, copying data directly between the two; there is no internal buffering.

func (*PipeReader) Close

func (r *PipeReader) Close() os.Error

Close closes the reader; subsequent writes to the write half of the pipe will return the error os.EPIPE.

func (*PipeReader) CloseWithError

func (r *PipeReader) CloseWithError(rerr os.Error) os.Error

CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error rerr.

func (*PipeReader) Read

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

type PipeWriter

Write half of pipe.

type PipeWriter struct {
    // contains unexported fields
}

func (*PipeWriter) Close

func (w *PipeWriter) Close() os.Error

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

func (*PipeWriter) CloseWithError

func (w *PipeWriter) CloseWithError(werr os.Error) os.Error

CloseWithError closes the writer; subsequent reads from the read half of the pipe will return no bytes and the error werr.

func (*PipeWriter) Write

func (w *PipeWriter) Write(data []byte) (n int, err os.Error)

Write implements the standard Write interface: it writes data to the pipe, blocking until 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 os.EPIPE.

type ReadByter

ReadByter is the interface that wraps the ReadByte method.

ReadByte reads and returns the next byte from the input. If no byte is available, err will be set.

type ReadByter interface {
    ReadByte() (c byte, err os.Error)
}

type ReadCloser

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

type ReadCloser interface {
    Reader
    Closer
}

type ReadSeeker

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

type ReadSeeker interface {
    Reader
    Seeker
}

type ReadWriteCloser

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

type ReadWriteCloser interface {
    Reader
    Writer
    Closer
}

type ReadWriteSeeker

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

type ReadWriteSeeker interface {
    Reader
    Writer
    Seeker
}

type ReadWriter

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

type ReadWriter interface {
    Reader
    Writer
}

type Reader

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 rather than block waiting for more.

At the end of the input stream, Read returns 0, os.EOF. Read may return a non-zero number of bytes with a non-nil err. In particular, a Read that exhausts the input may return n > 0, os.EOF.

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

func LimitReader

func LimitReader(r Reader, n int64) Reader

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

type ReaderAt

ReaderAt is the interface that wraps the basic ReadAt method.

ReadAt reads len(p) bytes into p starting at offset off in the underlying data stream. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered.

Even if ReadAt 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, ReadAt blocks until either all the data is available or an error occurs.

At the end of the input stream, ReadAt returns 0, os.EOF. ReadAt may return a non-zero number of bytes with a non-nil err. In particular, a ReadAt that exhausts the input may return n > 0, os.EOF.

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

type ReaderFrom

ReaderFrom is the interface that wraps the ReadFrom method.

type ReaderFrom interface {
    ReadFrom(r Reader) (n int64, err os.Error)
}

type SectionReader

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

type SectionReader struct {
    // contains unexported fields
}

func NewSectionReader

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

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

func (*SectionReader) Read

func (s *SectionReader) Read(p []byte) (n int, err os.Error)

func (*SectionReader) ReadAt

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

func (*SectionReader) Seek

func (s *SectionReader) Seek(offset int64, whence int) (ret int64, err os.Error)

func (*SectionReader) Size

func (s *SectionReader) Size() int64

Size returns the size of the section in bytes.

type Seeker

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: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. Seek returns the new offset and an Error, if any.

type Seeker interface {
    Seek(offset int64, whence int) (ret int64, err os.Error)
}

type WriteCloser

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

type WriteCloser interface {
    Writer
    Closer
}

type WriteSeeker

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

type WriteSeeker interface {
    Writer
    Seeker
}

type Writer

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

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

type WriterAt

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

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

type WriterTo

WriterTo is the interface that wraps the WriteTo method.

type WriterTo interface {
    WriteTo(w Writer) (n int64, err os.Error)
}

Subdirectories

Name   Synopsis
..
ioutil