Documentation Index
Fetch the complete documentation index at: https://mintlify.com/golang/go/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer object, creating another object (Reader or Writer) that also implements the interface but provides buffering and some help for textual I/O.Buffered Reading
NewReader
Returns a new Reader whose buffer has the default size (4096 bytes).Underlying reader to buffer
Buffered reader
NewReaderSize
Returns a new Reader whose buffer has at least the specified size.Underlying reader
Buffer size in bytes
Reader.Read
Reads data into p.Buffer to read into
Number of bytes read
Error encountered
Reader.ReadByte
Reads and returns a single byte.Reader.ReadString
Reads until the first occurrence of delim in the input.Delimiter byte to read until
String containing data up to and including the delimiter
Error (often io.EOF)
Reader.ReadBytes
Reads until the first occurrence of delim, returning a byte slice.Reader.ReadLine
Low-level line-reading primitive. Most callers should use ReadBytes(‘\n’) or ReadString(‘\n’) instead.Line data (without newline)
True if line was too long for buffer
Error encountered
Reader.Peek
Returns the next n bytes without advancing the reader.Number of bytes to peek
Reader.Buffered
Returns the number of bytes that can be read from the current buffer.Buffered Writing
NewWriter
Returns a new Writer whose buffer has the default size.Underlying writer to buffer
NewWriterSize
Returns a new Writer whose buffer has at least the specified size.Underlying writer
Buffer size in bytes
Writer.Write
Writes the contents of p into the buffer.Data to write
Number of bytes written
Error if write is short
Writer.WriteByte
Writes a single byte.Writer.WriteString
Writes a string.String to write
Writer.WriteRune
Writes a single Unicode code point.Rune to write
Writer.Flush
Writes any buffered data to the underlying io.Writer.Writer.Available
Returns how many bytes are unused in the buffer.Writer.Buffered
Returns the number of bytes that have been written into the current buffer.Scanner
NewScanner
Returns a new Scanner to read from r.Reader to scan
Scanner.Scan
Advances the Scanner to the next token.True if there is a token to read
Scanner.Text
Returns the most recent token as a string.Scanner.Bytes
Returns the most recent token as a byte slice.Scanner.Err
Returns the first non-EOF error encountered by the Scanner.Scanner.Split
Sets the split function for the Scanner.Function to tokenize input. Default is ScanLines.
Split Functions
Predefined split functions for Scanner:- ScanBytes - Returns each byte as a token
- ScanRunes - Returns each UTF-8 encoded rune as a token
- ScanWords - Returns each space-separated word as a token
- ScanLines - Returns each newline-terminated line as a token (default)
Complete Example
Performance Tips
- Always call
Flush()on Writer before closing the underlying writer - Use Scanner for simple line-by-line or word-by-word reading
- Use Reader methods for more control over buffering
- Adjust buffer size with NewReaderSize/NewWriterSize for optimal performance
- Scanner is more convenient but Reader gives more control