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 its stream that is sent over a Go channel as consecutive bytes:
type channelWriter struct { Channel chan byte } func NewChannelWriter() *channelWriter { return &channelWriter{ ...