Streaming Input/Output
There is flexibility and elegance in simplicity. Unlike several languages that decided to implement a feature-rich streaming framework, Go chose a simple capability-based approach: a reader is something from which you read bytes, and a writer is something to which you write bytes. In-memory buffers, files, network connections, and so on are all readers and writers, defined by io.Reader
and io.Writer
. A file is also an io.Seeker
, as you can randomly change the reading/writing location, but a network connection is not. A file and a network connection can be closed, so they are both io.Closer
, but a memory buffer is not. Such simple and elegant abstractions are the key to writing algorithms that can be used in different contexts.
In this chapter, we will look at some recipes showing how this capability-based streaming framework can be used idiomatically. We will also look at how to work with files and the filesystem. The recipes covered in this chapter are in...