Reading and writing to files
The most common scenario in DevOps tooling is the need to manipulate files: reading, writing, reformatting, or analyzing the data in those files. These files could be in many formats – JSON, YAML, XML, CSV, and others that are probably familiar to you. They are used to configure both local services and to interact with your cloud network provider.
In this section, we will cover the basics of reading and writing entire files.
Reading local files
Let's start by reading a configuration file on a local disk by using the os.Readfile()
function:
data, err := os.ReadFile("path/to/file")
The ReadFile()
method reads the location from its function parameter and returns that file's content. That return value is then stored in the data variable. An error is returned if the file cannot be read. For a refresher on errors, see the Handling errors in Go section in Chapter 2, Go Language Essentials.
ReadFile()
is a helper function...