You can get a number of errors, depending on how the connection was closed. The only error that you can count on receiving from a Read
is an io.EOF
. io.EOF
is the value use to indicate that a connection was closed normally.
Other errors can be checked against the net.Error
interface for its Timeout
and Temporary
methods. These are usually of the type net.OpError
. Any non-temporary error returned from a Write
is fatal, as it indicates the write couldn't succeed, but note that due to the underlying network API, writes returning no error still aren't guaranteed to have succeeded.
In general you can just follow the io.Reader
api.
When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.
If there was data read, you handle that first. After you handle the data, you can break from the loop on any error. If it was io.EOF
, the connection is closed normally, and any other errors you can handle as you see fit.