Skip to content

Instantly share code, notes, and snippets.

@orian
Last active January 27, 2023 15:29
Show Gist options
  • Save orian/b54ce2b1cc3a4144b090eb5714725a43 to your computer and use it in GitHub Desktop.
Save orian/b54ce2b1cc3a4144b090eb5714725a43 to your computer and use it in GitHub Desktop.
golang send a http request with a context deadline exceeded
package main
import (
"context"
"net/http"
)
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
ctx, _ := context.WithTimeout(context.Background(), 0)
r, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.google.pl", nil)
checkErr(err)
_, err = http.DefaultClient.Do(r)
// panic: Get "https://www.google.pl": context deadline exceeded
checkErr(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment