4

In golang, how would you handle error return by TCPConn.Read/Write? this document did not say anything about read/write errors.

zzn
  • 2,376
  • 16
  • 30
  • 1
    What specific errors it returns depends on the architecture. Handle it like any other error, though. – thwd Jul 17 '15 at 10:54

1 Answers1

8

Read and Write on a TCPConn could return most of errors that your combination of OS, hardware and drivers could return, along with some net package errors, and io.EOF when applicable. So it's really up to you to know what errors to look for, and how to handle them. Every layer of network API, even the berkeley socket api, is a leaky abstraction, and you can't make full use of it without understanding how the actual sockets and network protocols work.

Usually you only need be concerned with io.EOF, which is returned by Conn.Read when the connection is closed. On a TCP socket, this is the equivalent of recv returning 0 bytes on a blocking connection.

Any other errors that you could handle are usually wrapped in a net.Error, which provides the Temporary() and Timeout() methods. Often the real error type is a *net.OpError, which can provide even more information about when the error occurred, as well as encapsulate the actual error returned by the operation.

JimB
  • 104,193
  • 13
  • 262
  • 255
  • robust code must handle all errors correctly. For example, in Linux, in order to keep an alive tcp connection, some errors must redial the connection and others not. Just cann't mess it. nice answer. – zzn Jul 18 '15 at 01:37