Coding⏱️ 2 min read📅 2026-05-31

How to Fix: How to set timeout for http.Get() requests in Golang?

Set a custom timeout for http.Get() requests in Golang using the "Timeout" function.

Quick Answer: Use the "Timeout" function to set a custom timeout for each Get request, e.g. http.Get(url, 40*time.Second).

To set a custom timeout for each http.Get() request in Golang, you can utilize the Timeout function provided by the net/http package.

🛑 Root Causes of the Error

  • The default timeout for http.Get() requests is too long, causing your fetcher to be slow.

🔧 Proven Troubleshooting Steps

Method 1: Setting Timeout Using Timeout

  1. Step 1: Import the necessary package and create a new http.Client instance with the desired timeout.

Method 2: Setting Timeout Using Do Function

  1. Step 1: Use the http.Client instance to create a new Request with the desired timeout.

✨ Wrapping Up

To set a custom timeout for each http.Get() request in Golang, you can use either the Timeout function or the Do function. The recommended approach is to create a new http.Client instance with the desired timeout and then use this client to make requests.

Here's an example of how you can set a custom timeout using the Timeout function:

package main
import (

❓ Frequently Asked Questions