One thing I find myself often doing in just about any programming language I use is reading and writing small text files. We're talking text files that are only a few dozen lines long. Go provides the io/ioutils
package for quickly reading and writing files.
The two frequent use cases I have are these:
- Take a
string
or[]byte
and write it to a file. - Read a file into a
string
or[]byte
And there's a third case that comes up surprisingly often, too:
- Take any old
io.Reader
and read its entire contents into a[]byte
orstring
These are actually very simple Go operations provided by the io/ioutils
package. To illustrate, here's a simple program that starts with a string and file name, writes a file, and then reads the same file two different ways.
package main import ( "os" "io/ioutil" ) func main() { // Write a new file name := "test.txt" contents := []byte("These are the contents of the file.") if err := ioutil.WriteFile(name, contents, 0755); err != nil { panic(err) } // Read the entire contents of a file into a []byte results, err := ioutil.ReadFile(name) if err != nil { panic(err) } println(string(results)) // Or another useful tool: reader, err := os.Open(name) if err != nil { panic(err) } results, err = ioutil.ReadAll(reader) if err != nil { panic(err) } reader.Close() println(string(results)) }
The highlights of this are:
- The
iotul.WriteFile()
call, which takes astring
name, a[]byte
body, and anos.FileMode
(I cheated and just set its permission bits.) - The
ioutil.ReadFile()
function, which takes a filename and reads its contents, returning them as a[]byte
- The
ioutil.ReadAll()
function, which takes any oldio.Reader
(in this case anos.File
) and reads its entire contents into a[]byte
.
The io/ioutil
package has a few other useful functions. Along with these utilities, the bytes.Buffer
is another useful tool for working with chunks of data. Among other things, it provides a simple way for turning a string
or []byte
into a io.Reader
.