Documentation ¶
Overview ¶
Package ioutil implements some I/O utility functions.
Index ¶
- Variables
- func CheckClose(c io.Closer, err *error)
- func NewContextReadCloser(ctx context.Context, r io.ReadCloser) io.ReadCloser
- func NewContextReader(ctx context.Context, r io.Reader) io.Reader
- func NewContextWriteCloser(ctx context.Context, w io.WriteCloser) io.WriteCloser
- func NewContextWriter(ctx context.Context, w io.Writer) io.Writer
- func NewReadCloser(r io.Reader, c io.Closer) io.ReadCloser
- func NewReadCloserOnError(r io.ReadCloser, notify func(error)) io.ReadCloser
- func NewReadCloserWithCloser(r io.ReadCloser, c func() error) io.ReadCloser
- func NewReaderOnError(r io.Reader, notify func(error)) io.Reader
- func NewReaderUsingReaderAt(r io.ReaderAt, offset int64) io.Reader
- func NewWriteCloser(w io.Writer, c io.Closer) io.WriteCloser
- func NewWriteCloserOnError(w io.WriteCloser, notify func(error)) io.WriteCloser
- func NewWriterOnError(w io.Writer, notify func(error)) io.Writer
- func NonEmptyReader(r io.Reader) (io.Reader, error)
- func Pipe() (PipeReader, PipeWriter)
- func WriteNopCloser(w io.Writer) io.WriteCloser
- type PipeReader
- type PipeWriter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrEmptyReader = errors.New("reader is empty")
)
Functions ¶
func CheckClose ¶
CheckClose calls Close on the given io.Closer. If the given *error points to nil, it will be assigned the error returned by Close. Otherwise, any error returned by Close will be ignored. CheckClose is usually called with defer.
Example ¶
// CheckClose is commonly used with named return values f := func() (err error) { // Get a io.ReadCloser r := ioutil.NopCloser(strings.NewReader("foo")) // defer CheckClose call with an io.Closer and pointer to error defer CheckClose(r, &err) // ... work with r ... // if err is not nil, CheckClose will assign any close errors to it return err } err := f() if err != nil { panic(err) }
Output:
func NewContextReadCloser ¶
func NewContextReadCloser(ctx context.Context, r io.ReadCloser) io.ReadCloser
NewContextReadCloser as NewContextReader but with io.Closer interface.
func NewContextReader ¶
NewContextReader wraps a reader to make it respect given Context. If there is a blocking read, the returned Reader will return whenever the context is cancelled (the return values are n=0 and err=ctx.Err()).
func NewContextWriteCloser ¶
func NewContextWriteCloser(ctx context.Context, w io.WriteCloser) io.WriteCloser
NewContextWriteCloser as NewContextWriter but with io.Closer interface.
func NewContextWriter ¶
NewContextWriter wraps a writer to make it respect given Context. If there is a blocking write, the returned Writer will return whenever the context is cancelled (the return values are n=0 and err=ctx.Err()).
func NewReadCloser ¶
NewReadCloser creates an `io.ReadCloser` with the given `io.Reader` and `io.Closer`.
func NewReadCloserOnError ¶
func NewReadCloserOnError(r io.ReadCloser, notify func(error)) io.ReadCloser
NewReadCloserOnError returns a io.ReadCloser that call the notify function when an unexpected (!io.EOF) error happens, after call Read function.
func NewReadCloserWithCloser ¶
func NewReadCloserWithCloser(r io.ReadCloser, c func() error) io.ReadCloser
NewReadCloserWithCloser creates an `io.ReadCloser` with the given `io.ReaderCloser` and `io.Closer` that ensures that the closer is closed on close
func NewReaderOnError ¶
NewReaderOnError returns a io.Reader that call the notify function when an unexpected (!io.EOF) error happens, after call Read function.
func NewWriteCloser ¶
NewWriteCloser creates an `io.WriteCloser` with the given `io.Writer` and `io.Closer`.
func NewWriteCloserOnError ¶
func NewWriteCloserOnError(w io.WriteCloser, notify func(error)) io.WriteCloser
NewWriteCloserOnError returns a io.WriteCloser that call the notify function when an unexpected (!io.EOF) error happens, after call Write function.
func NewWriterOnError ¶
NewWriterOnError returns a io.Writer that call the notify function when an unexpected (!io.EOF) error happens, after call Write function.
func NonEmptyReader ¶
NonEmptyReader takes a reader and returns it if it is not empty, or `ErrEmptyReader` if it is empty. If there is an error when reading the first byte of the given reader, it will be propagated.
func Pipe ¶
func Pipe() (PipeReader, PipeWriter)
func WriteNopCloser ¶
func WriteNopCloser(w io.Writer) io.WriteCloser
WriteNopCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer w.
Types ¶
type PipeReader ¶
type PipeReader interface { io.ReadCloser CloseWithError(err error) error }
type PipeWriter ¶
type PipeWriter interface { io.WriteCloser CloseWithError(err error) error }