Buffered Readers
Read efficiently with bufio.
Why Buffer Reads
Reading one byte at a time from a file or network triggers a syscall every time. That is slow.
The bufio package wraps a reader and reads in large chunks into an in-memory buffer, serving your small reads from that buffer.
- Fewer syscalls
- Much faster for many small reads
Creating a bufio.Reader
Wrap any io.Reader with bufio.NewReader. Here we wrap a strings.Reader.
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
r := bufio.NewReader(strings.NewReader("Hello, Go!"))
fmt.Printf("buffered reader ready: %T\n", r)
}All lessons in this course
- Buffered Readers
- Scanners
- Buffered Writers
- Streaming Large Files