Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

Golang net/http Package – Detecting Timeout

Posted on March 16, 2019November 13, 2019 by admin

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

package main

import (
	"fmt"
	"net/http"
	"os"
	"time"
)

func main() {
	
	client := &http.Client{
		Timeout: time.Nanosecond * 1,
	}

	_, err := client.Get("https://google.com")

	isTimeout := false
	if os.IsTimeout(err) {
		isTimeout = true
	}

	fmt.Println("isTimeout)
}

Output: true

Follow @golangbyexample

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme