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 invalidUnreadByteusage.ErrInvalidUnreadRune: Error for invalidUnreadRuneusage.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 newio.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 nextnbytes without advancing the reader.Discard(n int) (int, error): Discards the nextnbytes.Read(p []byte) (int, error): Reads data intop.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 ofdelim.ReadLine() ([]byte, bool, error): Reads a single line.ReadBytes(delim byte) ([]byte, error): Reads until the first occurrence ofdelim, returning a slice containing the data up to and including the delimiter.ReadString(delim byte) (string, error): Reads until the first occurrence ofdelim, 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 newio.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 ofpinto 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:
Scannerfacilitates 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
ScanLinesas the split function unless overridden.
- Created using
-
Methods:
Scan() bool: Advances to the next token. Returnstrueif a token is found,falseat 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).
- Scanning stops at EOF, first I/O error, or if a token exceeds the buffer size (
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.