Last active
May 6, 2020 14:03
-
-
Save arxdsilva/53a1d4664d75f7b32917d1ccbfc0d84b to your computer and use it in GitHub Desktop.
request timeout in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"time" | |
) | |
func main() { | |
url := "http://localhost:3333/timeout" // fakeserver url | |
req, err := http.NewRequest("POST", url, nil) | |
if err != nil { | |
fmt.Println(1, err) | |
return | |
} | |
client := &http.Client{Timeout: time.Second} | |
res, err := client.Do(req) | |
if err != nil { | |
fmt.Println(2, err) | |
return | |
} | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
fmt.Println(3, err) | |
return | |
} | |
fmt.Println(err) | |
fmt.Println(string(body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment