How do I set timeouts and retries with resty in Go?

Go, Resty, HTTP client, timeouts, retries, Go programming
Learn how to set timeouts and retries using Resty in Go to ensure robust HTTP client behavior.

package main

import (
    "github.com/go-resty/resty/v2"
    "time"
    "fmt"
)

func main() {
    // Create a Resty client
    client := resty.New()

    // Set the timeout for the client
    client.SetTimeout(5 * time.Second) // Example timeout of 5 seconds

    // Set the retry count
    client.SetRetryCount(3) // Retry the request 3 times
    client.SetRetryWaitTime(2 * time.Second) // Wait 2 seconds between retries

    // Make a GET request
    resp, err := client.R().
        Get("https://example.com/api/resource")

    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("Response Status Code:", resp.StatusCode())
        fmt.Println("Response Body:", resp.String())
    }
}
    

Go Resty HTTP client timeouts retries Go programming