File i/o in Go
- 06 Feb, 2024
Printing in Golang
Go has many ways to print. All printing functions stem from the fmt
package.
There are 3 families of printing functions:
First family
fmt.Println
prints to os.Stdout by defaultfmt.Printf
allows you to format the string. For example%d
is the format code for integers.
Second family
fmt.Fprintln
can define an output identifier. For example os.Stdoutfmt.Fprintf
can define an output identifier. And also format the string.
Third family
fmt.Sprintln
outputs a string to a variable instead of an output identifierfmt.Sprintf
outputs to a string but also format the string.
File I/O Packages
- Os package: has functions to open and create files, list directories etc… and has the
File
type - Io package: has utilities to read and write
- Bufio pakcage: provides buffered i/o scanners etc…
- Io/ioutil package: has extra utilities such as reading and an entire file to memory, or writing it all out at once
- Strconv package: has utilities to convert to/from string representations
Misc commands for files:
- Opening a file:
os.Open(fname)
- Copying from a file to standard output:
io.Copy(os.Stdout, file)
- Reading all the data from a file (without buffering):
ioutil.ReadAll(file)
- Using bufio to scan a file: