package main import "fmt" import "time" func main() { t := time.NewTimer(2 * time.Second) //v := <- t.C //fmt.Println(v) go onTime(t.C) fmt.Println("main thread") time.Sleep(10 * time.Second) } func onTime(c <-chan time.Time) { for now := range c { // now := <- c fmt.Println("onTime", now) } }
package main import "fmt" import "time" func main() { time.AfterFunc(5 * time.Second, f1) time.AfterFunc(2 * time.Second, f2) fmt.Println("main thread") time.Sleep(10 * time.Second) } func f1() { fmt.Println("f1 done !") } func f2() { fmt.Println("f2 done !") }
package main import "fmt" import "time" var count int = 0 func main() { t := time.Tick(2 * time.Second) i := 0 for now := range t { fmt.Println(now, doSomething()) i++ if i > 10 { break } } } func doSomething() int { count++ return count }