0PricingLogin
Go Academy · Lesson

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

  1. Buffered Readers
  2. Scanners
  3. Buffered Writers
  4. Streaming Large Files
← Back to Go Academy