In addition to that, all goroutines exit when the main function exits. In 
your code, this happens when i == 5. This explains the output that you get. 
Both goroutines are able to produce five numbers until the main loop 
finishes and the main function exits.

On Monday, July 4, 2016 at 5:13:39 PM UTC+2, Jan Mercl wrote:
>
>
> On Mon, Jul 4, 2016 at 5:08 PM Kenshin Wang <[email protected] 
> <javascript:>> wrote:
>
>         go func(i int) {
>                 for {
>                         next <- i
>                         i++
>                 }
>         }(start)
>
> > could anyone can explain why the output is this?
>
> A goroutine dies when it returns. A goroutine with an endless loop is 
> immortal (modulo unhandled panics).
>
> -- 
>
> -j
>

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

Reply via email to