Misleading documentation for time.Timer?

146 views
Skip to first unread message

Johan Bolmsjö

unread,
Jul 5, 2023, 5:33:46 PM7/5/23
to golang-nuts
Hello,

By accident I came across the statement "Timer type represents a single event" when seeking inspiration for a timer implementation.
I wonder if this is true with the Timer.Reset method in mind, illustrated by https://play.golang.com/p/Kdg-xwmquQO.

This is probably the least important topic of the year but I hesitated creating a ticket for it as I'm not sure my idea of an event is what the comment refers to.

Documentation reference: https://pkg.go.dev/time#Timer

All the best,
Johan B

peterGo

unread,
Jul 5, 2023, 8:10:05 PM7/5/23
to golang-nuts
Johan,

For https://play.golang.com/p/Kdg-xwmquQO, timer has expired and Reset schedules f to run again and returns false.

Documentation reference: https://pkg.go.dev/time#Timer.Reset

Reset changes the timer to expire after duration d. It returns true if the timer had been active, false if the timer had expired or been stopped.

For a Timer created with AfterFunc(d, f), Reset either reschedules when f will run, in which case Reset returns true, or schedules f to run again, in which case it returns false.

Peter

Johan Bolmsjö

unread,
Jul 7, 2023, 12:22:23 PM7/7/23
to golang-nuts
Hi,

I understand how it works and I confess this is very pedantic.
It's the word "single" in the sentence "Timer type represents a single event" in the description of the Timer type that I object to.
If event corresponds to either the function given to AfterFunc or sending the time of expiry to the channel in the case it was created using NewTimer, then the event can be emitted more than a single time.

Sorry for the noise on this extremely minor detail.

All the best,
Johan B

Reply all
Reply to author
Forward
0 new messages