Use io.ReadWriter when you need to read from and write to the same stream, and io.ReadCloser when you need to read and explicitly close the resource. Wrap your underlying stream in bufio.NewReader to add buffering, then call Read or ReadByte methods on the returned reader.
import (
"bufio"
"io"
)
func processStream(rw io.ReadWriter) error {
buf := bufio.NewReader(rw)
data, err := buf.ReadBytes('\n')
if err != nil && err != io.EOF {
return err
}
_, err = rw.Write(data)
return err
}
For io.ReadCloser, ensure you call Close() when done to release resources:
func readFile(rc io.ReadCloser) error {
defer rc.Close()
buf := bufio.NewReader(rc)
_, err := buf.ReadBytes('\n')
return err
}