每间隔5s打印一句hello

// time_ticker
package main
import (
	"fmt"
	"time"
)
func main() {
	fmt.Println("Hello World!")
	interval := 5 * time.Second
	ticker := time.NewTicker(interval)
	for i := 0; i < 6; i++ {
		/* ticker.C是一个通道,时间到了,通道就可读了 */
		select {
		case <-ticker.C:
			fmt.Println(<-ticker.C, "hello")
		}
	}
}

输出结果

Hello World!
2019-04-04 13:16:09.601881 +0800 CST m=+10.005619032 hello
2019-04-04 13:16:19.602356 +0800 CST m=+20.005734375 hello
2019-04-04 13:16:29.600289 +0800 CST m=+30.003307220 hello
成功: 进程退出代码 0

定时器根据间隔时间写通道ticker.C, 所以可以监听通道ticker.C,一旦通道可读,就说明定时时间到了

05-19 08:34