Skip to content

Instantly share code, notes, and snippets.

@arxdsilva
Last active May 6, 2020 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arxdsilva/53a1d4664d75f7b32917d1ccbfc0d84b to your computer and use it in GitHub Desktop.
Save arxdsilva/53a1d4664d75f7b32917d1ccbfc0d84b to your computer and use it in GitHub Desktop.
request timeout in golang
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