We break down a basic (and very practical) timeout pattern in Go concurrency: add a timeout as one more event in
select via
time.After, and a controllable timer
time.NewTimer. We’ll show timeouts for
receive and
send, move the waiting logic into a function, discuss the
Stop() discipline, and common mistakes (trying to replace a timeout with
time.Sleep, uncontrolled
time.After in a “hot” loop, sender deadlock, and the false expectation that a timeout “cancels” the work of another goroutine).