Skip to content

Instantly share code, notes, and snippets.

@mallow111
Forked from superbrothers/main.go
Created June 22, 2017 22:14
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 mallow111/a5573988fb63d7d83d9c79d4c67ce24c to your computer and use it in GitHub Desktop.
Save mallow111/a5573988fb63d7d83d9c79d4c67ce24c to your computer and use it in GitHub Desktop.
http request with context in Go
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
)
func main() {
req, err := http.NewRequest("GET", "http://www.yahoo.co.jp", nil)
if err != nil {
log.Fatalf("%v", err)
}
ctx, cancel := context.WithTimeout(req.Context(), 1*time.Millisecond)
defer cancel()
req = req.WithContext(ctx)
client := http.DefaultClient
res, err := client.Do(req)
if err != nil {
log.Fatalf("%v", err)
}
fmt.Printf("%v\n", res.StatusCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment