package main
import (
"fmt"
"time"
)
func main() {
nowTime := time.Now()
fmt.Println(nowTime)
// 第一种定时器
time.Sleep(time.Second * 2)
nowTime = time.Now()
fmt.Println(nowTime)
// 创建第二种定时器
timeC := time.NewTimer(time.Second *2)
nowTime = <- timeC.C
fmt.Println(nowTime)
// 创建第三种定时器
nowTime = <- time.After(time.Second * 2)
fmt.Println(nowTime)
}
Demo二:
package main
import (
"fmt"
"time"
)
func main() {
// 第一种方式
go func () {
tickerC := time.NewTicker(time.Second * 3)
for {
nowTime := <- tickerC.C
fmt.Println("第一种方式:",nowTime)
}
}()
// 第二种方式
go func() {
for {
time.Sleep(time.Second *2)
fmt.Println("第二种方式")
}
}()
for {
;
}
}