The io.Writer interface

The io.Writer interface, as shown in the following code, is just as simple as its reader counterpart:

type Writer interface { 
   Write(p []byte) (n int, err error) 
} 

The interface requires the implementation of a single method, Write(p []byte)(c int, e error), that copies data from the provided stream p and writes that data to a sink resource such as an in-memory structure, standard output, a file, a network connection, or any number of io.Writer implementations that come with the Go standard library. The Write method returns the number of bytes copied from p followed by an error value if any was encountered.

The following code snippet shows the implementation of the channelWriter type, a writer that decomposes and serializes ...

Get Go: Design Patterns for Real-World Projects 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.