To deal with an HTTP timeout error in Go, use the os.IsTimeout()
operate from the built-in os
bundle. It returns true
if the request time restrict has been exceeded or false
in any other case.
Instance
Within the instance, we create an HTTP shopper with a timeout of 1 nanosecond. With such a brief timeout, we are able to make sure that we’ll obtain a timeout error after we ship a request to the https://instance.com
server.
|
|
Output:
2022/08/28 18:35:48 timeout error: Get "https://instance.com": context deadline exceeded (Shopper.Timeout exceeded whereas awaiting headers)
Learn this text to be taught extra concerning the
context deadline exceeded
, which can also be a timeout error.
To be sure that os.IsTimeout()
works accurately, change the timeout worth in line 11
to 1 * time.Minute
. If there may be at present no drawback with https://instance.com
, the request will probably be processed throughout the time restrict of 1 minute and you’ll not see any error on the output.