Really neat! Didn't know it was wrapped. Thank you.

On Wednesday, November 8, 2017 at 8:17:59 PM UTC+8, Jakob Borg wrote:
>
> Errors from net/http are wrapped in an url.Error to contain information 
> about the request that failed. You can unwrap it with an assertion or type 
> switch; 
>
> _, err := http.DefaultClient.Do(r)
> if urlErr, ok := err.(*url.Error); ok {
> log.Println(urlErr.Err == context.Canceled) // “true", in your example
> }
>
> //jb
>
>
> On 8 Nov 2017, at 13:11, Glen Huang <hey...@gmail.com <javascript:>> 
> wrote:
>
> Yes. Thanks for point it out, and maybe I should've mentioned that. 
>
> After posing that on SO, I have a gut feeling that it should be the 
> intuitive way to check that. I was hoping a go author could see the 
> question here and maybe offer some rationale why that's not the case.
>
> Sorry if it's considered spamming.
>
> On Wednesday, November 8, 2017 at 8:07:51 PM UTC+8, Ain wrote: 
>>
>>
>> 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 golang-nuts...@googlegroups.com <javascript:>.
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to