Within this tutorial, we are going to look at how you can effectively read and write to files within your filesystem using the go programming language.
The method we are going to use to read and write to these files will be file format-agnostic. What this means is that you’ll be able to use the techniques we’ll be covering in order to read and write,
.xls and so on, the only thing that differs for these files is the structure of the data that you write to each of these file types.
In order to read from files on your local filesystem, you’ll have to use the
io/ioutil module. You’ll first have to pull of the contents of a file into memory by calling
ioutil.ReadFile("/path/to/my/file.ext") which will take in the path to the file you wish to read in as it’s only parameter. This will return either the
data of the file, or an
err which can be handled as you normally handle errors in go.
Create a new file called
main.go as well as another file called
localfile.data. Add a random piece of text to the
.data file so that our finished go program has something to read and then do the following:
Once you have added this go code to your
main.go file, try running it by calling:
As you can see, we’ve successfully managed to read all of the data stored within our proprietary
localfile.data file type.
Writing Files to New Files
Now that we’ve covered reading from files in Go, it’s time to look at creating and writing to our own files!
In order to write content to files using Go, we’ll again have to leverage the
io/ioutil module. We’ll first have to construct a byte array that represents the content we wish to store within our files.
Once we have constructed this byte array, we can then call
ioutil.WriteFile() to write this byte array to a file. The
WriteFile() method takes in 3 different parameters, the first is the location of the file we wish to write to, the second is our
mydata object, and the third is the
FileMode, which represents our file’s mode and permission bits.
Let’s expand our original
main.go file to not only read, but to write to a file as well:
If you attempt to run this now by calling
go run main.go, you should see that a new file is automatically created within your current directory called
myfile.data and our go program proceeds to read from this newly created file and prints the contents in the console:
Writing to Existing Files
What happens if we have an existing file that we want to write additional information to?
Let’s take a look at that now.
Now that you have added the new code, we can test it out by running our
$ go run main.go All the data I wish to write to a file new data that wasn't there originally
And Voila! We have successfully managed to append to an existing file using
os.OpenFile and the
It’s incredibly important to understand the various different file permissions available to you when you are writing to new files.
Note - For more in-depth documentation about permissions, I would suggest checking out: https://golang.org/pkg/os/#FileMode
If you understand the basics of reading and writing files in Go, then you have the basics down for reading and writing any filetype possible, be that CSV, PNG or some proprietary data format. All you need to know, is how this data is structured so that you can parse and modify to suit your needs.
That’s all we are going to cover in this tutorial, we’ve managed to look at reading and writing to a really simple data format. We then briefly looked at file permissions.
Hopefully you found this tutorial useful, if you did, or if you require further help, then please do not hesitate to let me know in the comments section below!