Buffered Writers
Batch writes.
Buffered Writers is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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()
}Do Not Forget Flush
Without Flush(), your output may never appear. A common idiom is defer w.Flush().
package main
import (
"bufio"
"os"
)
func main() {
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
w.WriteString("written via defer\n")
}Writing Bytes and Strings
Use Write for byte slices, WriteString for strings, WriteByte and WriteRune for single values.
package main
import (
"bufio"
"os"
)
func main() {
w := bufio.NewWriter(os.Stdout)
w.Write([]byte("bytes "))
w.WriteString("string ")
w.WriteRune('!')
w.WriteByte('\n')
w.Flush()
}Checking Buffer State
Buffered() reports how many bytes are waiting, and Available() how much free space remains.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
w := bufio.NewWriter(os.Stdout)
w.WriteString("abc")
fmt.Fprintf(os.Stderr, "buffered: %d\n", w.Buffered())
w.Flush()
}Auto-Flush on Full
When the buffer fills, bufio.Writer flushes automatically. You still must Flush() at the end for the remaining partial buffer.
Custom Buffer Size
bufio.NewWriterSize(w, size) sets capacity. Larger buffers reduce flush frequency.
package main
import (
"bufio"
"os"
)
func main() {
w := bufio.NewWriterSize(os.Stdout, 8192)
w.WriteString("custom size buffer\n")
w.Flush()
}Formatted Writes with Fprintf
Because bufio.Writer is an io.Writer, you can pass it to fmt.Fprintf for formatted, buffered output.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
w := bufio.NewWriter(os.Stdout)
for i := 1; i <= 3; i++ {
fmt.Fprintf(w, "line %d\n", i)
}
w.Flush()
}Handling Write Errors
Errors may surface on a later write or at Flush(). Always check the error returned by Flush().
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
w := bufio.NewWriter(os.Stdout)
w.WriteString("data\n")
if err := w.Flush(); err != nil {
fmt.Fprintln(os.Stderr, "flush failed:", err)
}
}Batching Many Writes
The real win: a tight loop of small writes becomes one (or few) syscalls after buffering.
package main
import (
"bufio"
"os"
)
func main() {
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
for i := 0; i < 5; i++ {
w.WriteString("x")
}
w.WriteByte('\n')
}Best Practices
- Always
Flush()(ordeferit) - Check the error from
Flush(), not just from each write - Size the buffer to your workload
Quick Check
Test your buffered writer knowledge.
Recap
You learned to batch writes with bufio.Writer.
NewWriter/NewWriterSizeWrite,WriteString,WriteByte,WriteRune- Works with
fmt.Fprintf - Always
Flush()and check its error
Frequently asked questions
Is the “Buffered Writers” lesson free?
Yes — the full text of “Buffered Writers” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Buffered Writers”?
Batch writes. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Buffered Writers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Buffered Readers
- Scanners
- Buffered Writers
- Streaming Large Files