Reading and Writing to Files
Understand how reading and writing to a file works in Go.
We'll cover the following...
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 us. They are used to configure both local services and to interact with our cloud network provider.
In this lesson, we’ll 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.
ReadFile()
is a helper ...