https://play.golang.org/p/7FI8e7EVJBd
When for-range loop exits is it exit by return from gorotine?
In the above example it never prints line 15, why?
https://play.golang.org/p/7FI8e7EVJBd
When for-range loop exits is it exit by return from gorotine?
In the above example it never prints line 15, why?
The loop will never exit, as the ticker
s channel never gets closed.
Because the for
loop is infinite due to the fact that the ticker
s channel will never be closed.
hm, that behavior is quite confusing. it looks like a memory leaking.
The problem with closing is, if you had the following code, you’d get a problem if the ticker
s channel gets closed, as a closed channel always returns the zero-value:
for {
select {
case t <- ticker.C:
// ...
}
}
Though if you layed out your code in a different manor, ensuring that stopped tickers don’t get referenced anymore, then even the still left open channel will be GC’d eventually.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.