Golang time.Tick() function examples

package time

Golang time.Tick() function usage examples.

Example 1:

  package main

  import (
 "fmt"
 "time"
  )

  func echo(s string) {
 fmt.Println(s)
  }

  func delaySecond(n time.Duration) {
 for _ = range time.Tick(n * time.Second) {
 str := "Hi! " + n.String() + " seconds have passed"
 echo(str)
 }
  }

  func main() {

 go delaySecond(5) // very useful for interval polling

 select {} // this will cause the program to run forever
  }

Example 2:

 func statusUpdate() string { return "" }

 func ExampleTick() {
 c := time.Tick(1 * time.Minute)
 for now := range c {
 fmt.Printf("%v %s\n", now, statusUpdate())
 }
 }

Reference :

http://golang.org/pkg/time/#Tick

Advertisement