Formatted IO with fmt
One of the most widely used packages for IO is fmt
(https://golang.org/pkg/fmt). It comes with an amalgam of functions designed for formatted input and output. The most common usage of the fmt
package is for writing to standard output and reading from standard input. This section also highlights other functions that make fmt
a great tool for IO.
Printing to io.Writer interfaces
The fmt
package offers several functions designed to write text data to arbitrary implementations of io.Writer. The fmt.Fprint
and fmt.Fprintln
functions write text with the default format while fmt.Fprintf
supports format specifiers. The following code snippet writes a columnar formatted list of metalloid
data to a specified text file using the fmt.Fprintf
function:
type metalloid struct { name string number int32 weight float64 } func main() { var metalloids = []metalloid{ {"Boron", 5, 10.81}, ... ...