Lesson 20File I/O and OS Operations

Up to this point, we've been working with data within our programs that we either created in our code or have had users enter. It is also important to be able to save and retrieve data. In this lesson, we will cover file functionality provided in Go that will let us input data into and output it from our programs. We will also add coverage of working with directories. For an added bonus, we will cover the use of command-line arguments with our Go programs.

READING A FILE

You can perform standard input and output (I/O) operations on a file using the io/ioutil package in Go. This package provides routines to perform standard I/O operations such as reading and writing to files. The example in Listing 20.1 reads a file called flatland01.txt into memory and displays its contents as a string.

Get Job Ready Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.