On Mon, May 21, 2018 at 12:13 PM, <[email protected]> wrote:
> timeout := 1 * time.Millisecond
> select {
> case ch <-f(req):
> case <-time.After(timeout):
> ch <- errors.New("timeout")
> }
The instant 'select' is evaluated, ch is writable. Also time.After()
is called, and returns a channel that will be ready to be read in a
millisecond, i.e. not readable immediately. So it'll write to ch, and
will never timeout.
To do what you described, you need to run the request piece in a
goroutine, and write the result to a channel:
go func() {
_,err:=http.DefaultClient.Do(...)
ch<-err
}()
Then, wait for the error to arrive on ch, or timeout:
select {
case err:=<-ch:
case <-time.After(timeout):
}
> err:= <-ch
> close(ch)
> fmt.Println(err) // outputs <nil>, not timeout
> // Output:
> // timeout
> }
>
>
> --
> 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.
--
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.