HTTP Get with Timeout

· 133 words · 1 minute read

We’ve already covered how to download a file, but this post covers it a little further by specifying the maximum time attempted on the request. Adding timeouts are often important to stop your program coming to a halt.

To do this we make our own http client, giving it our custom timeout value. We can then use this client to make our requests. If a timeout is reached, the Get() function will return with an error. Our code is set to a very short timeout of 5 milliseconds to demo the error, but this can be set to any duration you need.

package main

import (

func main() {

    client := http.Client{
        Timeout: time.Duration(5 * time.Millisecond),

    _, err := client.Get("")
    if err != nil {

Image of Author Edd Turtle

Author: Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below. Thanks for reading!