Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Set a timeout while making an HTTP request in Go (Golang)

Posted on February 20, 2023February 20, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

client struct of HTTP package can be used to specify the timeout. While creating the HTTP client we can specify the value of the Timeout. An important thing to note about HTTP Client is that it is only created once and the same instance is used for making multiple HTTP requests. Below is the structure of http.Client struct

type Client struct {
Transport RoundTripper
CheckRedirect func(req *Request, via []*Request) error
Jar CookieJar
Timeout time.Duration
}

As you can see there is an option to specify the timeout for the http.Client which is the Timeout field

Now let’s see a working example of this

Program

In the below program we are setting a timeout of 1 nanosecond to showcase that timeout is indeed happening while making the request.

package main
import (
"fmt"
"net/http"
"os"
"time"
)
func main() {
client := &http.Client{
Timeout: time.Nanosecond * 1,
}
_, err := client.Get("https://google.com")
if os.IsTimeout(err) {
fmt.Println("Timeout Happened")
} else {
fmt.Println("Timeout Did not Happened")
}
}

Output

Timeout Happened
  • go
  • golang
  • 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

    ©2023 Welcome To Golang By Example | Design: Web XP