Skip to content

time: document that Tick interval gap can exceed duration #30271

@xsigndll

Description

@xsigndll

What version of Go are you using (go version)?

go version go1.11.5 windows/amd64

What did you do?

The documentation example for time.Tick ( https://golang.org/pkg/time/#Tick ) states that the channel returns now. But if the routine inside the tick exceeds the duration, the ticker produces a one-time gap. This may need clarification in the documentation example. You may check this playground example: https://play.golang.org/p/ImIhZTLYciq

package main

import (
	"fmt"
	"time"
)

func main() {
	c := time.Tick(1 * time.Second)
	for now := range c {
		fmt.Printf("now\t\t%v\n", now)
		fmt.Printf("time.Now()\t%v\n", time.Now())
		time.Sleep(3 * time.Second)
	}
}

What did you expect to see?

time.Now() should be the same as now.

What did you see instead?

It produces a gap in the second interval:

now		2009-11-10 23:00:01 +0000 UTC m=+1.000000001
time.Now()	2009-11-10 23:00:01 +0000 UTC m=+1.000000001
now		2009-11-10 23:00:02 +0000 UTC m=+2.000000001
time.Now()	2009-11-10 23:00:04 +0000 UTC m=+4.000000001  <==== +2s
now		2009-11-10 23:00:05 +0000 UTC m=+5.000000001
time.Now()	2009-11-10 23:00:07 +0000 UTC m=+7.000000001
now		2009-11-10 23:00:08 +0000 UTC m=+8.000000001
time.Now()	2009-11-10 23:00:10 +0000 UTC m=+10.000000001
now		2009-11-10 23:00:11 +0000 UTC m=+11.000000001
time.Now()	2009-11-10 23:00:13 +0000 UTC m=+13.000000001

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions