Timing out with channels
One somewhat critical point with channels and select loops that we haven't examined particularly closely is the ability—and often necessity—to kill a select loop after a certain timeout.
Many of the applications we've written so far are long-running or perpetually-running, but there are times when we'll want to put a finite time limit on how long goroutines can operate.
The for { select { } } switch we've used so far will either live perpetually (with a default case) or wait to be broken from one or more of the cases.
There are two ways to manage interval-based tasks—both as part of the time package, unsurprisingly.
The time.Ticker struct allows for any given operation after the specified period of time. It provides C, a blocking ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access