← All languages
Go function of the day
Random

bufio.NewWriter

Create a buffered writer.

Description

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.

Arguments

NameDescriptionOptional
w The io.Writer to buffer. No

Example

f, _ := os.Create("output.txt")
defer f.Close()
w := bufio.NewWriter(f)
w.WriteString("buffered output
")
w.Flush()

Reference