Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Time, Math and Randomness

Go time.After() and time.NewTimer() functions

Reference for Go time.After, time.NewTimer, time.AfterFunc, Stop and Reset: timeouts in select, cancelling timers, and the Go 1.23 timer changes.

A timer fires once, after a delay. Go gives you three ways to make one. time.After(d) returns a channel that receives the time after d; time.NewTimer(d) returns a *time.Timer you can stop and reset; and time.AfterFunc(d, f) runs f in its own goroutine after d.

The classic use is a timeout inside a select: wait for a result on one channel, or give up when the timer channel fires.

Go
select {
case res := <-results:
	fmt.Println(res)
case <-time.After(2 * time.Second):
	fmt.Println("timed out")
}

Try it

Go A timeout in select

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Stop, Reset and AfterFunc

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func After(d Duration) <-chan Time
func NewTimer(d Duration) *Timer
func AfterFunc(d Duration, f func()) *Timer
func (t *Timer) Stop() bool
func (t *Timer) Reset(d Duration) bool
Name Description
d Delay before the timer fires. Zero or negative fires immediately.
f Function to run in its own goroutine (AfterFunc only).
Timer.C Channel that receives the time when the timer fires. nil for AfterFunc timers.

Returns: After returns a channel. NewTimer and AfterFunc return a *Timer. Stop returns true if the call stopped the timer before it fired, false if it had already fired or been stopped. Reset reports the same about the previous state.

Usage notes

  • After cannot be cancelled. Before Go 1.23 an After timer stayed alive, holding memory, until it fired, which mattered in hot loops with long delays. Since Go 1.23 unreferenced timers are collected, but NewTimer plus Stop is still clearer when you may not need the timer.
  • Go 1.23 changed the semantics (for programs whose language version is 1.23 or later): the timer channel is unbuffered, and after Stop or Reset returns you will never receive a stale value. The old advice to drain t.C before Reset is no longer needed.
  • With older semantics, a timer that already fired leaves a value in C; code that reset timers had to drain it. Keep that in mind when reading older code.
  • AfterFunc callbacks run on their own goroutine, so they must be safe against races with the code that created them. Stop does not wait for a callback already running.
  • Prefer context.WithTimeout when the deadline should travel through function calls; see context.WithTimeout().
  • time.Sleep blocks the current goroutine for a duration and is fine when you have nothing else to wait on.
  • Timeouts abandon, they do not cancel. In the first example the slow goroutine keeps running after the timeout. Give it a context or a done channel if it should stop.