Create a buffered writer.
bufio.NewWriter returns a new Writer whose buffer has the default size (4096 bytes). It wraps an io.Writer and provides buffered writing, which can significantly improve performance by reducing the number of system calls.
Data written to the buffered writer is accumulated in the buffer and only flushed to the underlying writer when the buffer is full or when Flush is called explicitly.
Always call Flush after finishing writing to ensure all buffered data is written to the underlying writer.
| Name | Description | Optional |
|---|---|---|
w |
The io.Writer to buffer. | No |
f, _ := os.Create("output.txt")
defer f.Close()
w := bufio.NewWriter(f)
w.WriteString("buffered output
")
w.Flush()