【速查版】Go 标准库:bufio

https://golang.org/pkg/bufio/ 官方标准库文档不好阅读,遂整理了下,方便自己快速查阅。

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. 【不是每次调用 Read 都从 Reader 读数据,而是预先从 Reader 读一部分数据放到缓冲区,后续的 Read 从缓冲区中取数据】

函数

扫描器 split 函数

ScanBytes,按字节扫描

ScanBytes is a split function for a Scanner that returns each byte as a token.

func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error)
ScanLines,按行扫描

ScanLines is a split function for a Scanner that returns each line of text, stripped of any trailing end-of-line marker. The last non-empty line of input will be returned even if it has no newline.

func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error)
ScanRunes,按 rune 扫描
func ScanRunes(data []byte, atEOF bool) (advance int, token []byte, err error)

ScanRunes is a split function for a Scanner that returns each UTF-8-encoded rune as a token.

ScanWords,按单词扫描

ScanWords is a split function for a Scanner that returns each space-separated word of text, with surrounding spaces deleted. It will never return an empty string.

func ScanWords(data []byte, atEOF bool) (advance int, token []byte, err error)

返回结构体的函数

NewReader

NewReader returns a new Reader whose buffer has the default size.

func NewReader(rd io.Reader) *Reader
NewReaderSize

NewReaderSize returns a new Reader whose buffer has at least the specified size. If the argument io.Reader is already a Reader with large enough size, it returns the underlying Reader.

func NewReaderSize(rd io.Reader, size int) *Reader
NewWriter

NewWriter returns a new Writer whose buffer has the default size.

func NewWriter(w io.Writer) *Writer
NewWriterSize

NewWriterSize returns a new Writer whose buffer has at least the specified size. If the argument io.Writer is already a Writer with large enough size, it returns the underlying Writer.

func NewWriterSize(w io.Writer, size int) *Writer
NewReadWriter

NewReadWriter allocates a new ReadWriter that dispatches to r and w.

func NewReadWriter(r *Reader, w *Writer) *ReadWriter
NewScanner

结构体

带缓冲的读

Reader

Reader implements buffering for an io.Reader object.

type Reader struct {
    // contains filtered or unexported fields
}
Buffered

Buffered returns the number of bytes that can be read from the current buffer.

func (b *Reader) Buffered() int
Size

Size returns the size of the underlying buffer in bytes.

func (b *Reader) Size() int
Reset

Reset discards any buffered data, resets all state, and switches the buffered reader to read from r.

func (b *Reader) Reset(r io.Reader)
Discard

Discard skips the next n bytes, returning the number of bytes discarded.

func (b *Reader) Discard(n int) (discarded int, err error)
Peek

Peek returns the next n bytes without advancing the reader. The bytes stop being valid at the next read call. If Peek returns fewer than n bytes, it also returns an error explaining why the read is short. The error is ErrBufferFull if n is larger than b’s buffer size.

func (b *Reader) Peek(n int) ([]byte, error)
Read

Read reads data into p. It returns the number of bytes read into p. The bytes are taken from at most one Read on the underlying Reader, hence n may be less than len§.

func (b *Reader) Read(p []byte) (n int, err error)
ReadByte

ReadByte reads and returns a single byte. If no byte is available, returns an error.

func (b *Reader) ReadByte() (byte, error)
ReadBytes

ReadBytes reads until the first occurrence of delim in the input, returning a slice containing the data up to and including the delimiter.

func (b *Reader) ReadBytes(delim byte) ([]byte, error)
ReadLine
func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error)

ReadLine is a low-level line-reading primitive. Most callers should use ReadBytes(’\n’) or ReadString(’\n’) instead or use a Scanner.

ReadRune
func (b *Reader) ReadRune() (r rune, size int, err error)

ReadRune reads a single UTF-8 encoded Unicode character and returns the rune and its size in bytes.

ReadSlice
func (b *Reader) ReadSlice(delim byte) (line []byte, err error)

ReadSlice reads until the first occurrence of delim in the input, returning a slice pointing at the bytes in the buffer. The bytes stop being valid at the next read.

ReadString
func (b *Reader) ReadString(delim byte) (string, error)

ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter.

UnreadByte
func (b *Reader) UnreadByte() error

UnreadByte unreads the last byte. Only the most recently read byte can be unread.

UnreadRune
func (b *Reader) UnreadRune() error

UnreadRune unreads the last rune.

WriteTo
func (b *Reader) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriterTo. This may make multiple calls to the Read method of the underlying Reader. If the underlying reader supports the WriteTo method, this calls the underlying WriteTo without buffering.

带缓冲的写

Writer

Writer implements buffering for an io.Writer object. If an error occurs writing to a Writer, no more data will be accepted and all subsequent writes, and Flush, will return the error. After all data has been written, the client should call the Flush method to guarantee all data has been forwarded to the underlying io.Writer.

Available

Available returns how many bytes are unused in the buffer.

func (b *Writer) Available() int
Buffered

Buffered returns the number of bytes that have been written into the current buffer.

func (b *Writer) Buffered() int
Size

Size returns the size of the underlying buffer in bytes.

func (b *Writer) Size() int
Reset

Reset discards any unflushed buffered data, clears any error, and resets b to write its output to w.

func (b *Writer) Reset(w io.Writer)
Flush

Flush writes any buffered data to the underlying io.Writer.

func (b *Writer) Flush() error
ReadFrom
func (b *Writer) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implements io.ReaderFrom. If the underlying writer supports the ReadFrom method, and b has no buffered data yet, this calls the underlying ReadFrom without buffering.

Write
func (b *Writer) Write(p []byte) (nn int, err error)

Write writes the contents of p into the buffer. It returns the number of bytes written. If nn < len§, it also returns an error explaining why the write is short.

WriteByte
WriteRune
WriteString

带缓冲的读和写

ReadWriter

扫描器

Scanner
Buffer
func (s *Scanner) Buffer(buf []byte, max int)

Buffer sets the initial buffer to use when scanning and the maximum size of buffer that may be allocated during scanning. The maximum token size is the larger of max and cap(buf). If max <= cap(buf), Scan will use this buffer only and do no allocation.

By default, Scan uses an internal buffer and sets the maximum token size to MaxScanTokenSize.

Split
func (s *Scanner) Split(split SplitFunc)

Split sets the split function for the Scanner. The default split function is ScanLines.

Scan
func (s *Scanner) Scan() bool

Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method. It returns false when the scan stops, either by reaching the end of the input or an error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil. Scan panics if the split function returns too many empty tokens without advancing the input. This is a common error mode for scanners.

Err
func (s *Scanner) Err() error

Err returns the first non-EOF error that was encountered by the Scanner.

Bytes
func (s *Scanner) Bytes() []byte

Bytes returns the most recent token generated by a call to Scan. The underlying array may point to data that will be overwritten by a subsequent call to Scan. It does no allocation.

Text
func (s *Scanner) Text() string

Text returns the most recent token generated by a call to Scan as a newly allocated string holding its bytes.

split函数

SplitFunc
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

SplitFunc is the signature of the split function used to tokenize the input. The arguments are an initial substring of the remaining unprocessed data and a flag, atEOF, that reports whether the Reader has no more data to give. The return values are the number of bytes to advance the input and the next token to return to the user, if any, plus an error, if any.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值