io

package
v0.1.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TextDelim is the default delimiter for multiline strings.
	TextDelim string = `"""`
)

Variables

View Source
var (
	// ErrUnread occurs when an unread method is called on a scanner
	// whose previous operation was not a successful read.
	//
	// Format:
	//    "nothing to unread"
	ErrUnread error = errors.New("nothing to unread")

	// ErrEOF is the error returned when the reader returns [io.EOF].
	//
	// Callees must return this error as is and not wrap it as callers
	// are expected to check for this error with the == operator.
	//
	// Format:
	//   "end of file"
	ErrEOF error = errors.New("end of file")
)

Functions

func IsEOF

func IsEOF(err error) bool

IsEOF reports whether the error is an ErrEOF or an io.EOF error. Nil errors are considered to be EOF errors.

Parameters:

  • err: The error to check.

Returns:

  • bool: `true` if the error is an EOF error or nil, `false` otherwise.

func ScanLine

func ScanLine(scanner *bufio.Scanner) (string, error)

ScanLine reads a single line from the provided scanner.

Parameters:

  • scanner: The scanner to read from.

Returns:

  • string: The line read from the scanner.
  • error: An error if reading from the scanner fails.

Errors:

  • errors.ErrBadParam: If the scanner is nil.
  • any other error: Depends on the call to bufio.Scanner.Scan.

func ScanText

func ScanText(scanner *bufio.Scanner) iter.Seq2[string, error]

ScanText returns an iterator that reads a multiline string from the provided scanner that is delimited by TextDelim. If the input text is not started with TextDelim, the iterator returns only the first line.

The iterator is not single-use and each line in the multiline string is yielded, all except for the delimiters. If an error occurs, the iterator is closed and the error is returned.

Parameters:

  • scanner: The scanner to read from.

Returns:

  • iter.Seq2[string, error]: An iterator that yields a multiline string and an error. It is never nil.

func WriteBytes

func WriteBytes(w io.Writer, p []byte) error

WriteBytes writes the given bytes to the given writer.

Parameters:

  • w: The writer to write to.
  • p: The bytes to write.

Returns:

  • error: An error if writing to the writer fails.

Errors:

  • common.ErrBadParam: If `w` is `nil`.
  • io.ErrShortWrite: If the string is not fully written to the writer.
  • any other error: Depends on the call to io.Writer.Write method.

func WriteCString

func WriteCString(w Writer, str string) error

WriteCString is like WriteString but it writes a null-terminated string. Of course, this means the string must not contain a null byte within it.

Parameters:

  • w: The writer to write the string to.
  • str: The string to write.

Returns:

  • error: An error if writing fails.

Errors:

  • errors.ErrBadParam: If the writer is nil.
  • any other error: Depends on the call to io.Writer.Write.

func WriteRune

func WriteRune(w io.Writer, char rune) error

WriteRune is like WriteBytes but writes a single rune.

Parameters:

  • w: The writer to write the rune to.
  • char: The rune to write.

Returns:

  • error: An error if the write operation fails.

Errors:

  • errors.ErrBadParam: If `w` is nil.
  • any other error: Depends on the `io.Writer` implementation.

func WriteString

func WriteString(w Writer, str string) error

WriteString is a helper function that writes a string to a writer. Does nothing if the string is empty.

If the writer supports StringWriter, then it will be used. Otherwise, the string will be written as bytes.

Parameters:

  • w: The writer to write the string to.
  • str: The string to write.

Returns:

  • error: An error if writing to the writer fails.

Errors:

  • errors.ErrBadParam: If the writer is nil.
  • any other error: Depends on the call to io.Writer.Write.

Types

type ByteReader

type ByteReader interface {
	// ReadByte reads a single byte and returns it. If no byte is
	// available, err will be set.
	//
	// Returns:
	//   - byte: The byte read.
	//   - error: An error if reading from the reader fails.
	//
	// Errors:
	//   - any error: Depends on the implementation.
	ReadByte() (byte, error)
}

ByteReader is an interface that represents a reader that can read bytes.

type ByteScanner

type ByteScanner interface {
	// UnreadByte causes the next call to [ReadByte] to return the last
	// byte read.
	//
	// If the last operation was not a successful call to ReadByte,
	// UnreadByte may return an error, unread the last byte read (or the
	// byte prior to the last-unread byte), or (in implementations that
	// support the [Seeker] interface) seek to the start of the byte
	// before the current offset.
	//
	// Errors:
	//   - any error: Depends on the implementation.
	UnreadByte() error

	ByteReader
}

ByteScanner is an interface that represents a reader that can read bytes.

type Reader

type Reader interface {
	// Read reads up to len(p) bytes. Even if [Read] returns less than
	// 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 instead of waiting for
	// more. When [Read] encounters an error or EOF condition after
	// successfully reading n > 0 bytes, it returns the number of bytes
	// read (0 <= n <= len(p)).
	//
	// Callers should always process the n > 0 bytes returned before
	// considering the error err. Doing so correctly handles I/O errors
	// that happen after reading some bytes and also both of the allowed
	// EOF behaviors.
	//
	// If len(p) == 0, [Read] should return n == 0. However, it may
	// return a non-nil error if some error condition is known, such as
	// EOF.
	//
	// Implementations of [Read] are discouraged from returning a zero
	// byte count with a nil error, except when len(p) == 0. Callers
	// should treat a return of 0 and nil as indicating that nothing
	// happened; in particular it does not indicate EOF.
	//
	// Implementations must not retain p.
	//
	// Parameters:
	//   - p: The buffer to read into.
	//
	// Returns:
	//   - int: The number of bytes read. Always non-negative.
	//   - error: An error if reading from the reader fails.
	//
	// Errors:
	//   - any error: Depends on the implementation.
	Read(p []byte) (int, error)
}

Reader is the interface for byte sourced readers.

type RuneReader

type RuneReader interface {
	// ReadRune reads a single encoded Unicode character and returns the
	// rune and its size in bytes. If no character is available, err will
	// be set.
	//
	// Returns:
	//   - rune: The rune read.
	//   - int: The number of bytes read.
	//   - error: An error if reading from the reader fails.
	//
	// Errors:
	//   - any error: Depends on the implementation.
	ReadRune() (rune, int, error)
}

RuneReader is an interface that represents a reader that can read encoded Unicode characters.

type RuneScanner

type RuneScanner interface {
	// UnreadRune causes the next call to [ReadRune] to return the last
	// rune read.
	//
	// If the last operation was not a successful call to ReadRune,
	// UnreadRune may return an error, unread the last rune read (or the
	// rune prior to the last-unread rune), or (in implementations that
	// support the [Seeker] interface) seek to the start of the rune
	// before the current offset.
	//
	// Errors:
	//   - any error: Depends on the implementation.
	UnreadRune() error

	RuneReader
}

RuneScanner is an interface that represents a reader that can read encoded Unicode characters.

type StringWriter

type StringWriter interface {
	// WriteString is like [Write] but it writes a string. All the
	// properties of [Write] must be met.
	//
	// Parameters:
	//   - str: The string to write.
	//
	// Returns:
	//   - int: The number of bytes written.
	//   - error: An error if writing to the writer fails.
	//
	// Errors:
	//   - any error: Depends on the implementation.
	WriteString(str string) (int, error)
}

StringWriter is an interface that represents a writer that can write strings.

type Writer

type Writer interface {
	// 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).
	// [Write] must not modify the slice data, even temporarily.
	//
	// Implementations must not retain p.
	//
	// Parameters:
	//   - p: The bytes to write.
	//
	// Returns:
	//   - int: The number of bytes written.
	//   - error: An error if writing to the writer fails.
	//
	// Errors:
	//   - any error: Depends on the implementation.
	Write(p []byte) (int, error)
}

Writer is an interface that represents a writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL