I noticed that OP has also posted this question to SO:
https://stackoverflow.com/questions/47179024/how-to-check-if-a-request-was-cancelled
ain
kolmapäev, 8. november 2017 14:02.13 UTC+2 kirjutas Glen Huang:
>
> I have this simple code in which I try to check if the request was
> cancelled. But surprisingly, it prints false instead of true in go 1.9.
>
> I wonder what's the correct way to check that?
>
> package main
>
>
> import (
> "context"
> "log"
> "net/http"
> )
>
>
> func main() {
> r, _ := http.NewRequest("GET", "http://example.com", nil)
> ctx := context.Background()
> ctx, cancel := context.WithCancel(ctx)
> r = r.WithContext(ctx)
> ch := make(chan bool)
> go func() {
> _, err := http.DefaultClient.Do(r)
> log.Println(err == context.Canceled)
> ch <- true
> }()
> cancel()
> <-ch
> }
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.