Golang HTTP Contexto
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://jsonplaceholder.typicode.com/users/1", nil)
defer ctx.Done()
if err != nil {
log.Fatal(err)
}
client := http.DefaultClient
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
fmt.Println(string(data))
Restu Wahyu Saputra