Timers – running something later
If you want to do something later, use time.Timer
. A Timer
is a nice way of doing the following:
// This is only for illustration. Don't do this! type TimerMockup struct { C chan<- time.Time } func NewTimerMockup(dur time.Duration) *TimerMockup { t := &TimerMockup{ C: make(chan time.Time,1), } go func() { // Sleep, and then send to the channel time.Sleep(dur) t.C <- time.Now() }() return t }
So, a timer is like a goroutine that will send a message to a channel after sleeping for...