User Tools

Site Tools


go:stdlib:bufio

bufio package

What is it?

`bufio` implements buffered I/O by wrapping an `io.Reader` or `io.Writer` and using an in-memory buffer to make reading/writing more efficient.

What is it used for?

  • Faster reading/writing for files and network connections.
  • Convenient scanning of lines/tokens (`bufio.Scanner`).
  • Convenient line-based reads (`ReadString`, `ReadBytes`).

Common types

  • `bufio.Reader`: buffered reader
  • `bufio.Writer`: buffered writer
  • `bufio.Scanner`: token/line scanner

Example: buffered reading (lines)

r := bufio.NewReader(f)
line, err := r.ReadString('\n')
_ = line
_ = err

Example: buffered writing

w := bufio.NewWriter(f)
_, _ = w.WriteString("hello\n")
_ = w.Flush()

Notes / pitfalls

  • Always call `Flush()` on `bufio.Writer` to ensure data is written.
  • `Scanner` has a token size limit by default; for big lines, configure buffer or use `Reader`.

Hard words (English)

  • buffer /ˈbʌfər/: bộ đệm
  • wrap /ræp/: bọc
  • efficient /ɪˈfɪʃənt/: hiệu quả
  • flush /flʌʃ/: đẩy hết dữ liệu trong buffer ra nơi ghi
  • token /ˈtoʊkən/: đơn vị tách (mảnh dữ liệu)
go/stdlib/bufio.txt · Last modified: by phong2018