Useful I/O packages in Go
The io
package is for performing primitive file I/O operations, whereas the bufio
package is for executing buffered I/O.
Note
In buffered I/O, the operating system uses an intermediate buffer during file read and write operations in order to reduce the number of filesystem calls. As a result, buffered input and output is faster and more efficient.
Additionally, you can use some of the functions of the fmt
package to write text to a file. Note that the flag
package will be also used in this chapter as well as in all the forthcoming ones where the developed utilities need to support command-line flags.
The io package
The io
package offers functions that allow you to write to or read from files. Its use will be illustrated in the usingIO.go
file, which will be presented in three parts. What the program does is read 8
bytes from a file and write them in a standard output.
The first part is the preamble of the Go program:
package main import ( "fmt" "io" "os...