Working with the io package

The obvious place to start with IO is, well, the io package (https://golang.org/pkg/io). As we have already seen, the io package defines input and output primitives as the io.Reader and io.Writer interfaces. The following table summarizes additional functions and types, available in the io package, that facilitate streaming IO operations.

Function

Description

io.Copy()

The io.Copy function (and its variants io.CopyBuffer and io.CopyN) make it easy to copy data from an arbitrary io.Reader source into an equally arbitrary io.Writer sink as shown in the following snippet:

data := strings.NewReader("Write   me down.")   
file, _ := os.Create("./iocopy.data")   
io.Copy(file, data)   

golang.fyi/ch10/iocopy.go

PipeReader PipeWriter ...

Get Learning Go Programming 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.