Buffered IO
Most IO operations covered so far have been unbuffered. This implies that each read and write operation could be negatively impacted by the latency of the underlying OS to handle IO requests. Buffered operations, on the other hand, reduces latency by buffering data in internal memory during IO operations. The bufio
package (https://golang.org/pkg/bufio/) offers functions for buffered read and write IO operations.
Buffered writers and readers
The bufio
package offers several functions to do buffered writing of IO streams using an io.Writer
interface. The following snippet creates a text file and writes to it using buffered IO:
func main() { rows := []string{ "The quick brown fox", "jumps over the lazy dog", } fout, err := os.Create("./filewrite.data") writer := bufio.NewWriter(fout) if err != nil { fmt.Println(err) os.Exit(1) } defer fout.Close() for _, row := range rows { ...