Considering what is mentioned in the video, it looks like whenever a second 
has gone by and a message is not returned only then the exit from loop  
will happen.
Which means the loop may run indefinitely as well if boring function keeps 
on returning something or the other within a second on the channel returned 
by it..

On Friday, September 23, 2016 at 8:08:43 PM UTC+5:30, DM wrote:
>
> Thanks Sam for the reply. 
>
> If I get you correctly then the entire loop will run for 1 second right 
> because after 1second then time.After will return the current time on the 
> channel and the function will return.
>
> On Sep 23, 2016 7:43 PM, "Sam Whited" <s...@samwhited.com <javascript:>> 
> wrote:
>
> On Fri, Sep 23, 2016 at 6:24 AM, DM <subhara...@gmail.com <javascript:>> 
> wrote:
> > So a timer is started for 1 second. Before 1 second is over let's say 
> boring writes something to channel c then case s := <- c will be executed. 
> The moment somthing is written on channel c the timer that was started gets 
> garbage collected and therefore it never returns the current time on the 
> channel after 1 second?
> >
> >     for {
> >         select {
> >         case s := <-c:
> >             fmt.Println(s)
> >         case <-time.After(1 * time.Second):
> >             fmt.Println("You're too slow.")
> >             return
> >         }
> >     }
>
> I think there's some confusion here about how the select statement
> works; when you receive something from c in this channel, it is
> printed, and then the loop starts over and we wait on input again. If
> both c and the channel returned by time.After have nothing to receive,
> we put the current goroutine to sleep. When *either* one of them
> receives some data (eg. you've sent something else to c, or the timer
> has expired and sent on the channel returned by time.After), that case
> is selected and executed. If both c and the channel returned by
> time.After have something, a case is selected randomly. The time.After
> channel isn't garbage collected until after the function returns (when
> it goes out of scope). Since we're in an infinite loop, this only
> happens after the time.After channel is selected and the return is
> hit.
>
> The main thing to remember is that select puts the goroutine to sleep
> until any of its case statements unblock (data is available to read
> from the channel, in this case). When one or more of the case
> staetments unblock it picks one at random and executes it, then moves
> on (which in this case means trying again because it's in an infinite
> loop).
>
> —Sam
>
>
> --
> Sam Whited
> pub 4096R/54083AE104EA7AD3
>
>
>

-- 
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