Golang 中的类似定时器功能的警报

有什么方法可以设计一个在 Golang 中的特定未来时间到期的计时器?我的意思是一个在凌晨 2 点到期的计时器(让当前时间为凌晨 12 点)。我知道一种方法是使用,

 timer(target_future_time - current_time)

但似乎不是一种确切的方法(考虑到执行时间可能不准确)。有人可以帮忙吗?


一只萌萌小番薯
浏览 165回答 2
2回答

MM们

在golang中,可能有两种方式创建ticker,如下所示:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; //第一种实现方式&nbsp;&nbsp; &nbsp; ticker1 := time.NewTicker(1 * time.Second)&nbsp; &nbsp; i := 1&nbsp; &nbsp; for c := range ticker1.C {&nbsp; &nbsp; &nbsp; &nbsp; i++&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(c.Format("2006/01/02 15:04:05.999999999"))&nbsp; &nbsp; &nbsp; &nbsp; if i > 5 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker1.Stop()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(time.Now().Format("2006/01/02 15:04:05.999999999"), " 1 Finished.")&nbsp; &nbsp; //第二种实现方式&nbsp;&nbsp; &nbsp; i = 1&nbsp; &nbsp; ticker2 := time.AfterFunc(1*time.Second, func() {&nbsp; &nbsp; &nbsp; &nbsp; i++&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(time.Now().Format("2006/01/02 15:04:05.999999999"))&nbsp; &nbsp; })&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case <-ticker2.C:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("nsmei")&nbsp; &nbsp; &nbsp; &nbsp; case <-time.After(3 * time.Second):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i <= 5 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker2.Reset(1 * time.Second)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto BRK&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; BRK:&nbsp; &nbsp; &nbsp; &nbsp; ticker2.Stop()&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(time.Now().Format("2006/01/02 15:04:05.999999999"), " 2 Finished.")}输出:2016/01/26 16:46:34.2612485672016/01/26 16:46:35.2563817432016/01/26 16:46:36.2597171522016/01/26 16:46:37.2603208372016/01/26 16:46:38.2593127042016/01/26 16:46:38.259410752&nbsp; 1 Finished.2016/01/26 16:46:39.2606042742016/01/26 16:46:42.2610913222016/01/26 16:46:45.2631362572016/01/26 16:46:48.2641935172016/01/26 16:46:51.2656551372016/01/26 16:46:53.265722632&nbsp; 2 Finished.根据执行,第一个比第二个更精确。在您的情况下,您可以使用time.Time.Sub()计算持续时间,并使用第二种方法执行一次,其余使用第一种方法。我希望这些对你有帮助!

慕斯709654

下面的代码达到了你的目的。您所要做的就是按照下面 main() 函数中显示的方式调用 ScheduleAlarm()。package mainimport (&nbsp; &nbsp; "strings"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")// A struct for representing time.type Time struct {&nbsp; &nbsp; Hh int // Hours.&nbsp; &nbsp; Mm int // Minutes.&nbsp; &nbsp; Ss int // Seconds.}func main() {&nbsp; &nbsp; /*&nbsp; &nbsp; Set the alarm as shown below.&nbsp; &nbsp; Time must be specified in 24 hour clock format.&nbsp; &nbsp; Also, pass the callback to be called after the alarm is triggered.&nbsp; &nbsp; */&nbsp; &nbsp; alarm := ScheduleAlarm(Time{23, 28, 0}, func() {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("alarm received")&nbsp; &nbsp; })&nbsp; &nbsp; // Do your stuff.&nbsp; &nbsp; for i := 0; i < 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(i)&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(2 * time.Second)&nbsp; &nbsp; }&nbsp; &nbsp; // Don't forget to call the below line whenever you want to block.&nbsp; &nbsp; <-alarm}// Call this function to schedule the alarm. The callback will be called after the alarm is triggered.func ScheduleAlarm(alarmTime Time, callback func() ()) (endRecSignal chan string) {&nbsp; &nbsp; endRecSignal = make(chan string)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; timeSplice := strings.Split(time.Now().Format("15:04:05"), ":")&nbsp; &nbsp; &nbsp; &nbsp; hh, _ := strconv.Atoi(timeSplice[0])&nbsp; &nbsp; &nbsp; &nbsp; mm, _ := strconv.Atoi(timeSplice[1])&nbsp; &nbsp; &nbsp; &nbsp; ss, _ := strconv.Atoi(timeSplice[2])&nbsp; &nbsp; &nbsp; &nbsp; startAlarm := GetDiffSeconds(Time{hh, mm, ss}, alarmTime)&nbsp; &nbsp; &nbsp; &nbsp; // Setting alarm.&nbsp; &nbsp; &nbsp; &nbsp; time.AfterFunc(time.Duration(startAlarm) * time.Second, func() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endRecSignal <- "finished recording"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; close(endRecSignal)&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }()&nbsp; &nbsp; return}func GetDiffSeconds(fromTime, toTime Time) int {&nbsp; &nbsp; fromSec := GetSeconds(fromTime)&nbsp; &nbsp; toSec := GetSeconds(toTime)&nbsp; &nbsp; diff := toSec - fromSec&nbsp; &nbsp; if diff < 0 {&nbsp; &nbsp; &nbsp; &nbsp; return diff + 24 * 60 * 60&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return diff&nbsp; &nbsp; }}func GetSeconds(time Time) int {&nbsp; &nbsp; return time.Hh * 60 * 60 + time.Mm * 60 + time.Ss}希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go