我希望每天晚上9点打印出do my job
。在Go语言中,我该如何做到这一点?
这是我目前已经得到的:
timer := time.NewTimer(3 * time.Second)
for {
now := time.Now()
next := now.Add(time.Hour * 24)
todayNine := time.Date(next.Year(), next.Month(), next.Day(), 9, 0, 0, 0, next.Location()).AddDate(0, 0, -1)
todayFifteen := time.Date(next.Year(), next.Month(), next.Day(), 15, 0, 0, 0, next.Location()).AddDate(0, 0, -1)
todayEnd := time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location()).AddDate(0, 0, -1)
if now.Before(todayNine) {
timer.Reset(todayNine.Sub(now))
} else if now.Before(todayFifteen) {
timer.Reset(todayFifteen.Sub(now))
} else if now.Before(todayEnd) {
timer.Reset(todayEnd.Sub(now))
}
<- timer.C
fmt.Println("do my job")
}