set timeout for clients
This commit is contained in:
parent
43825687bb
commit
c2fdbaf6ce
34
examples/http-clients/http-clients-with-timeout.go
Normal file
34
examples/http-clients/http-clients-with-timeout.go
Normal file
@ -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)
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user