3 ms·
No, it operates more like: ticker := time.NewTicker(time.Minute) for { now, ok := <-ticker.C if !ok { break }
by ominous_prime 13y ago
No, it operates more like:
ticker := time.NewTicker(time.Minute)
for {
now, ok := <-ticker.C
if !ok {
break
}
fmt.Printf("%s is the current time\n", now)
}
Your two loops are superficially equivalent only because the ticker isn't going to close its channel. Plus, you're creating a new ticker on every iteration in your second example, and you don't need the select statement, because your only blocking on a single receive operation.
The range over a channel is especially useful for a channel that will be closed, so you can break from the loop.