第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

Golang 中的類似定時(shí)器功能的警報(bào)

Golang 中的類似定時(shí)器功能的警報(bào)

Go
一只萌萌小番薯 2021-12-07 16:06:43
有什么方法可以設(shè)計(jì)一個(gè)在 Golang 中的特定未來時(shí)間到期的計(jì)時(shí)器?我的意思是一個(gè)在凌晨 2 點(diǎn)到期的計(jì)時(shí)器(讓當(dāng)前時(shí)間為凌晨 12 點(diǎn))。我知道一種方法是使用, timer(target_future_time - current_time)但似乎不是一種確切的方法(考慮到執(zhí)行時(shí)間可能不準(zhǔn)確)。有人可以幫忙嗎?
查看完整描述

2 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

在golang中,可能有兩種方式創(chuàng)建ticker,如下所示:


package main


import (

    "fmt"

    "time"

)


func main() {

    //第一種實(shí)現(xiàn)方式 

    ticker1 := time.NewTicker(1 * time.Second)

    i := 1

    for c := range ticker1.C {

        i++

        fmt.Println(c.Format("2006/01/02 15:04:05.999999999"))

        if i > 5 {

            ticker1.Stop()

            break

        }

    }

    fmt.Println(time.Now().Format("2006/01/02 15:04:05.999999999"), " 1 Finished.")


    //第二種實(shí)現(xiàn)方式 

    i = 1

    ticker2 := time.AfterFunc(1*time.Second, func() {

        i++

        fmt.Println(time.Now().Format("2006/01/02 15:04:05.999999999"))

    })


    for {

        select {

        case <-ticker2.C:

            fmt.Println("nsmei")

        case <-time.After(3 * time.Second):

            if i <= 5 {

                ticker2.Reset(1 * time.Second)

                continue

            }

            goto BRK

        }

    BRK:

        ticker2.Stop()

        break

    }

    fmt.Println(time.Now().Format("2006/01/02 15:04:05.999999999"), " 2 Finished.")

}

輸出:


2016/01/26 16:46:34.261248567

2016/01/26 16:46:35.256381743

2016/01/26 16:46:36.259717152

2016/01/26 16:46:37.260320837

2016/01/26 16:46:38.259312704

2016/01/26 16:46:38.259410752  1 Finished.

2016/01/26 16:46:39.260604274

2016/01/26 16:46:42.261091322

2016/01/26 16:46:45.263136257

2016/01/26 16:46:48.264193517

2016/01/26 16:46:51.265655137

2016/01/26 16:46:53.265722632  2 Finished.

根據(jù)執(zhí)行,第一個(gè)比第二個(gè)更精確。


在您的情況下,您可以使用time.Time.Sub()計(jì)算持續(xù)時(shí)間,并使用第二種方法執(zhí)行一次,其余使用第一種方法。


我希望這些對你有幫助!


查看完整回答
反對 回復(fù) 2021-12-07
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

下面的代碼達(dá)到了你的目的。您所要做的就是按照下面 main() 函數(shù)中顯示的方式調(diào)用 ScheduleAlarm()。


package main


import (

    "strings"

    "strconv"

    "fmt"

    "time"

)


// A struct for representing time.

type Time struct {

    Hh int // Hours.

    Mm int // Minutes.

    Ss int // Seconds.

}


func main() {

    /*

    Set the alarm as shown below.

    Time must be specified in 24 hour clock format.

    Also, pass the callback to be called after the alarm is triggered.

    */

    alarm := ScheduleAlarm(Time{23, 28, 0}, func() {

        fmt.Println("alarm received")

    })


    // Do your stuff.

    for i := 0; i < 10; i++ {

        fmt.Println(i)

        time.Sleep(2 * time.Second)

    }


    // Don't forget to call the below line whenever you want to block.

    <-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) {

    endRecSignal = make(chan string)

    go func() {

        timeSplice := strings.Split(time.Now().Format("15:04:05"), ":")

        hh, _ := strconv.Atoi(timeSplice[0])

        mm, _ := strconv.Atoi(timeSplice[1])

        ss, _ := strconv.Atoi(timeSplice[2])


        startAlarm := GetDiffSeconds(Time{hh, mm, ss}, alarmTime)


        // Setting alarm.

        time.AfterFunc(time.Duration(startAlarm) * time.Second, func() {

            callback()

            endRecSignal <- "finished recording"

            close(endRecSignal)

        })

    }()

    return

}


func GetDiffSeconds(fromTime, toTime Time) int {

    fromSec := GetSeconds(fromTime)

    toSec := GetSeconds(toTime)

    diff := toSec - fromSec


    if diff < 0 {

        return diff + 24 * 60 * 60

    } else {

        return diff

    }

}


func GetSeconds(time Time) int {

    return time.Hh * 60 * 60 + time.Mm * 60 + time.Ss

}

希望這可以幫助。


查看完整回答
反對 回復(fù) 2021-12-07
  • 2 回答
  • 0 關(guān)注
  • 215 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號