go语言中的bufio包(记忆总结)

78 阅读2分钟

Go's bufio Package

The bufio package in Go provides buffered I/O wrappers for reading and writing, which helps to improve efficiency when dealing with I/O operations. Here's a summary of the key elements and functionalities provided by the bufio package:

Constants and Variables

  • Constants:

    • defaultBufSize: Default buffer size (4096 bytes).
    • minReadBufferSize: Minimum read buffer size (16 bytes).
    • maxConsecutiveEmptyReads: Maximum number of consecutive empty reads (100).
  • Variables:

    • ErrInvalidUnreadByte: Error for invalid UnreadByte usage.
    • ErrInvalidUnreadRune: Error for invalid UnreadRune usage.
    • ErrBufferFull: Error when the buffer is full.
    • ErrNegativeCount: Error for negative count.

Buffered Input (Reader)

The Reader struct provides buffered reading capabilities. Key methods include:

  • Constructor Methods:

    • NewReaderSize(rd io.Reader, size int) *Reader: Creates a new buffered reader with the specified buffer size.
    • NewReader(rd io.Reader) *Reader: Creates a new buffered reader with the default buffer size.
  • Buffer Management:

    • Size() int: Returns the buffer size.
    • Reset(r io.Reader): Resets the reader to read from a new io.Reader.
    • Buffered() int: Returns the number of bytes that can be read from the current buffer.
  • Reading Methods:

    • Peek(n int) ([]byte, error): Returns the next n bytes without advancing the reader.
    • Discard(n int) (int, error): Discards the next n bytes.
    • Read(p []byte) (int, error): Reads data into p.
    • ReadByte() (byte, error): Reads and returns a single byte.
    • UnreadByte() error: Unreads the last byte read.
    • ReadRune() (rune, int, error): Reads a single UTF-8 encoded Unicode character.
    • UnreadRune() error: Unreads the last rune read.
  • Line and Delimiter Reading:

    • ReadSlice(delim byte) ([]byte, error): Reads until the first occurrence of delim.
    • ReadLine() ([]byte, bool, error): Reads a single line.
    • ReadBytes(delim byte) ([]byte, error): Reads until the first occurrence of delim, returning a slice containing the data up to and including the delimiter.
    • ReadString(delim byte) (string, error): Reads until the first occurrence of delim, returning a string containing the data up to and including the delimiter.
  • Writing Methods:

    • WriteTo(w io.Writer) (int64, error): Writes data to the specified writer.

Buffered Output (Writer)

The Writer struct provides buffered writing capabilities. Key methods include:

  • Constructor Methods:

    • NewWriterSize(w io.Writer, size int) *Writer: Creates a new buffered writer with the specified buffer size.
    • NewWriter(w io.Writer) *Writer: Creates a new buffered writer with the default buffer size.
  • Buffer Management:

    • Size() int: Returns the buffer size.
    • Reset(w io.Writer): Resets the writer to write to a new io.Writer.
    • Flush() error: Writes any buffered data to the underlying writer.
    • Available() int: Returns the number of bytes that are unused in the buffer.
    • AvailableBuffer() []byte: Returns an empty buffer with available capacity.
    • Buffered() int: Returns the number of bytes that have been written into the current buffer.
  • Writing Methods:

    • Write(p []byte) (int, error): Writes the contents of p into the buffer.
    • WriteByte(c byte) error: Writes a single byte.
    • WriteRune(r rune) (int, error): Writes a single Unicode rune.

Scanner (Scanner)

The Scanner in the bufio package provides a convenient way to read data such as newline-delimited lines of text from an input source. Here's a concise summary:

  • Purpose: Scanner facilitates tokenizing input data, stepping through tokens defined by a split function. Common tokens include lines, bytes, UTF-8-encoded runes, and space-delimited words.

  • Initialization:

    • Created using NewScanner(io.Reader).
    • Defaults to ScanLines as the split function unless overridden.
  • Methods:

    • Scan() bool: Advances to the next token. Returns true if a token is found, false at EOF or error.
    • Bytes() []byte: Returns the most recent token as a byte slice.
    • Text() string: Returns the most recent token as a string.
  • Customization:

    • Split(SplitFunc): Sets a custom split function for tokenization.Such as: ScanRunes,ScanWords,ScanBytes,ScanLine。
    • Buffer([]byte, int): Sets the initial buffer and maximum size allowed for buffering tokens.
  • Error Handling:

    • Err() error: Returns the first non-EOF error encountered during scanning.
  • Error Types:

    • ErrTooLong: Token exceeds maximum size.
    • ErrNegativeAdvance, ErrAdvanceTooFar: Split function errors related to invalid advances.
    • ErrBadReadCount: Read operation returned an impossible count.
  • Termination:

    • Scanning stops at EOF, first I/O error, or if a token exceeds the buffer size (Scanner.Buffer).

This Scanner provides a structured way to read and process data in various token formats, handling errors and termination conditions gracefully.

Summary

The bufio package enhances the efficiency of I/O operations in Go by providing buffered reading and writing. By reducing the number of I/O operations, it helps to improve performance, especially in scenarios where multiple small reads or writes are performed. The package includes various methods to manage buffers and handle different types of I/O scenarios effectively.