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 ...
Get Go: Building Web Applications now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.