0PricingLogin
Go Academy · Lesson

Buffered Writers

Batch writes.

Why Buffer Writes

Like reads, every small write can trigger a syscall. bufio.Writer collects writes in memory and flushes them in large chunks.

Critical: buffered data is lost unless you Flush().

Creating a bufio.Writer

Wrap any io.Writer. Here we wrap os.Stdout.

package main

import (
	"bufio"
	"os"
)

func main() {
	w := bufio.NewWriter(os.Stdout)
	w.WriteString("Hello buffered\n")
	w.Flush()
}

All lessons in this course

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