From c2fdbaf6ce021c0f6911dc0279b38494131a27f4 Mon Sep 17 00:00:00 2001 From: Selin Kurt Date: Mon, 21 Oct 2019 20:55:37 +0300 Subject: [PATCH] set timeout for clients --- .../http-clients/http-clients-with-timeout.go | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/http-clients/http-clients-with-timeout.go diff --git a/examples/http-clients/http-clients-with-timeout.go b/examples/http-clients/http-clients-with-timeout.go new file mode 100644 index 0000000..5185139 --- /dev/null +++ b/examples/http-clients/http-clients-with-timeout.go @@ -0,0 +1,34 @@ +// In this example, we set timeout for clients + +package main + +import ( + "fmt" + "net/http" + "time" +) + +func sendClient(url string) { + + client := http.Client{ + Timeout: time.Duration(10 * time.Second), + } + + resp, err := client.Get(url) + + if err != nil { + fmt.Println("ERROR: Failed to Client \"" + url + "\"") + return + } + + // fmt.Println(resp) + defer resp.Body.Close() + +} + +func main() { + + url := "https://gobyexample.com/" + sendClient(url) + +}