Select and Timeouts

select waits on several channels at once and runs the one that is ready first.

The idea

You are waiting for a parcel, a phone call, or an alarm clock, whichever comes first. You do not stare at one door; you sit and wait for any of them, then deal with that one and forget the rest.

select does exactly that with channels. time.After(d) is a channel that delivers a value after d, so using it as one case gives you a timeout. A default case means “do not wait at all”.

Watch it work

Try it

Common mistakes