{"id":9,"date":"2019-03-16T13:57:30","date_gmt":"2019-03-16T13:57:30","guid":{"rendered":"https:\/\/golangbyexamples.com\/\/?p=9"},"modified":"2019-11-13T16:09:53","modified_gmt":"2019-11-13T16:09:53","slug":"net-http-timeout","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/net-http-timeout\/","title":{"rendered":"Golang net\/http Package – Detecting Timeout"},"content":{"rendered":"\n
Below is a sample code piece to know if there is a timeout for an upstream call that is using net\/http package. Http Client is created with 1 nanosecond timeout so that it always times out on google.com <\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"net\/http\"\n\t\"os\"\n\t\"time\"\n)\n\nfunc main() {\n\t\n\tclient := &http.Client{\n\t\tTimeout: time.Nanosecond * 1,\n\t}\n\n\t_, err := client.Get(\"https:\/\/google.com\")\n\n\tisTimeout := false\n\tif os.IsTimeout(err) {\n\t\tisTimeout = true\n\t}\n\n\tfmt.Println(\"isTimeout)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong> true<\/p>\n","protected":false},"excerpt":{"rendered":"Below is a sample code piece to know if there is a timeout for an upstream call that is using net\/http package. Http Client is created with 1 nanosecond timeout so that…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\n
Golang net\/http Package - Detecting Timeout - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n