Question:I am using the
conn.SetReadDeadlinemethod to set the read timeout for
conn.Readwaits for more than the specified time, it will return and return an error of type
*net.OpError. This error is returned by the
netpackage after wrapping all non-
I can get the error before wrapping with
Unwrap(). A timeout error is an error of type
*poll.DeadlineExceededError. I use statements like this in my code to handle timeout errors precisely.
use of internal package internal/poll not allowederror while running the program. The compiler tells me that internal packages cannot be used.
I googled and found a solution to delete the
internalfolder, is this the final solution? Will there be a better solution?
ospackage exports that error as
os.ErrDeadlineExceeded(check the source code). You can try :
If the deadline is exceeded a call to Read or Write or to other I/O methods will return an error that wraps os.ErrDeadlineExceeded. This can be tested using errors.Is(err, os.ErrDeadlineExceeded).
Another indication that an error is a “timeout” error is if it has a
Timeout() boolmethod, which returns
This is part of the
net.Errorinterface (although this interface has an extra method, which is documented as deprecated), and implemented by the
net.OpErrortype (and also by the internal
There are several functions (
os.SyscallError.IsTimeout()and the public function
os.IsTimeout()for example) that implement a timeout checking by directly casting the error value to a
If you want to deal with errors that can be wrapped one into another, you may want to implement your own
note that :
the difference between the
isTimeout()function above and the
errors.Is(err, os.ErrDeadlineExceeded)call is that the latter will try to match exacly an
ErrDeadlineExceeded(which is mostly triggered by setting
SetDeadline()on some file-or-conn-like object), while the latter one may return true for errors that try to advertise “I’m a timeout error” for other reasons (e.g : an HTTP “408 Request timeout” response)
note : all links above refer to go 1.18.3 . Depending on the version of go you are using, you may have to adapt some of the code above (for example : the
os.ErrDeadlineExceededwas added in go1.15 )
If you have better answer, please add a comment about this, thank you!