Categorygithub.com/jarxorg/io2
modulepackage
0.7.1
Repository: https://github.com/jarxorg/io2.git
Documentation: pkg.go.dev

# README

github.com/jarxorg/io2

PkgGoDev Report Card Coverage Status

Go "io" package utilities.

Delegator

Delegator implements io.Reader, io.Writer, io.Seeker, io.Closer. Delegator can override the I/O functions that is useful for unit tests.

package main

import (
  "bytes"
  "errors"
  "fmt"
  "io/ioutil"

  "github.com/jarxorg/io2"
)

func main() {
  org := bytes.NewReader([]byte(`original`))

  r := io2.DelegateReader(org)
  r.ReadFunc = func(p []byte) (int, error) {
    return 0, errors.New("custom")
  }

  var err error
  _, err = ioutil.ReadAll(r)
  fmt.Printf("Error: %v\n", err)

  // Output: Error: custom
}

No-op Closer

// NopReadCloser returns a ReadCloser with a no-op Close method wrapping the provided interface.
// This function like io.NopCloser(io.Reader).
func NopReadCloser(r io.Reader) io.ReadCloser {
  return DelegateReader(r)
}

// NopReadWriteCloser returns a ReadWriteCloser with a no-op Close method wrapping the provided interface.
func NopReadWriteCloser(rw io.ReadWriter) io.ReadWriteCloser {
  return DelegateReadWriter(rw)
}

// NopReadSeekCloser returns a ReadSeekCloser with a no-op Close method wrapping the provided interface.
func NopReadSeekCloser(r io.ReadSeeker) io.ReadSeekCloser {
  return DelegateReadSeeker(r)
}

// NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided interface.
func NopWriteCloser(w io.Writer) io.WriteCloser {
  return DelegateWriter(w)
}

WriteSeeker

WriteSeekBuffer implements io.Writer, io.Seeker and io.Closer. NewWriteSeekBuffer(capacity int) returns the buffer.

// WriteSeekCloser is the interface that groups the basic Write, Seek and Close methods.
type WriteSeekCloser interface {
  io.Writer
  io.Seeker
  io.Closer
}
package main

import (
  "fmt"
  "io"

  "github.com/jarxorg/io2"
)

func main() {
  o := io2.NewWriteSeekBuffer(16)
  o.Write([]byte(`Hello!`))
  o.Truncate(o.Len() - 1)
  o.Write([]byte(` world!`))

  fmt.Println(string(o.Bytes()))

  o.Seek(-1, io.SeekEnd)
  o.Write([]byte(`?`))

  fmt.Println(string(o.Bytes()))

  // Output:
  // Hello world!
  // Hello world?
}

Multi Readers

io2 provides MultiReadCloser, MultiReadSeeker, MultiReadSeekCloser.

package main

import (
  "fmt"
  "io"
  "io/ioutil"
  "strings"

  "github.com/jarxorg/io2"
)

func main() {
  r, _ := io2.NewMultiReadSeeker(
    strings.NewReader("Hello !"),
    strings.NewReader(" World"),
  )

  r.Seek(5, io.SeekStart)
  p, _ := ioutil.ReadAll(r)
  fmt.Println(string(p))

  r.Seek(-5, io.SeekEnd)
  p, _ = ioutil.ReadAll(r)
  fmt.Println(string(p))

  // Output:
  // ! World
  // World
}

# Functions

Delegate returns a Delegator with the provided io interfaces (io.Reader, io.Seeker, io.Writer, io.Closer).
DelegateReadCloser returns a Delegator with the provided Read and Close functions.
DelegateReader returns a Delegator with the provided Read function.
DelegateReadSeekCloser returns a Delegator with the provided Read, Seek and Close functions.
DelegateReadSeeker returns a Delegator with the provided Read and Seek functions.
DelegateReadWriteCloser returns a Delegator with the provided Read, Write and Close functions.
DelegateReadWriter returns a Delegator with the provided Read and Write functions.
DelegateReadWriteSeeker returns a Delegator with the provided Read, Write and Seek functions.
DelegateWriteCloser returns a Delegator with the provided Write and Close functions.
DelegateWriter returns a Delegator with the provided Write function.
DelegateWriteSeekCloser returns a Delegator with the provided Write, Seek and Close functions.
DelegateWriteSeeker returns a Delegator with the provided Write and Seek functions.
No description provided by the author
NewMultiReadCloser create a ReaderCloser that's the logical concatenation of the provided input readers.
NewMultiReader creates a Reader that's the logical concatenation of the provided input readers.
NewMultiReadSeekCloser creates a ReadSeekCloser that's the logical concatenation of the provided input readers.
NewMultiReadSeeker creates a ReadSeeker that's the logical concatenation of the provided input readers.
No description provided by the author
NewWriteSeekBuffer returns an WriteSeekBuffer with the initial capacity.
NewWriteSeekBufferBytes returns an WriteSeekBuffer with the initial buffer.
NopReadCloser returns a ReadCloser with a no-op Close method wrapping the provided interface.
NopReadSeekCloser returns a ReadSeekCloser with a no-op Close method wrapping the provided interface.
NopReadWriteCloser returns a ReadWriteCloser with a no-op Close method wrapping the provided interface.
NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided interface.

# Variables

ErrNotImplemented "not implemented".

# Structs

Delegator implements Reader, Writer, Seeker, Closer.
WriteSeekBuffer implements io.WriteSeeker that using in-memory byte buffer.

# Interfaces

MultiReadCloser is the interface that groups the MultiReader and Close methods.
MultiReader represents a multiple reader.
MultiReadSeekCloser is the interface that groups the MultiReadSeeker and Close methods.
MultiReadCloser is the interface that groups the MultiReader, Seek and SeekReader methods.
WriteSeekCloser is the interface that groups the basic Write, Seek and Close methods.