Most DevOps work touches files: config files, logs, certificates,
templates. The simplest way to read one in Go is os.ReadFile, which
slurps the whole file into memory in a single call.
It takes a path and returns two values: the file contents and an error.
data, err := os.ReadFile("/etc/app.conf")
if err != nil {
fmt.Println("could not read file:", err)
return
}
Reading a file can fail for many ordinary reasons: the path is wrong,
the file was deleted, or the process lacks permission. The error is not
optional noise, it is how you find out. Follow the same if err != nil
rhythm you use everywhere else and handle the failure before you touch
data.
The shape to memorise is one call followed by an error check:
data, err := os.ReadFile(path)
if err != nil {
// handle it
}