该函数每小时精确返回一个小时的值

I'm trying to create a function that will return a value exactly on the hour every hour. The code below it what I've got so far but is there a better approach I could take?

func main() {
    for {
            t := time.Now()
            if t.Minute() == 00 {
                fmt.Println("Hello World!")
            }
            time.Sleep(1 * time.Minute)
        }
    }

You can use the time package AfterFunc function.

time.AfterFunc(time.Hour * 1, func() {
  // Do something!
})

It returns a Timer that can be used to cancel the call using its Stop method. The first call you might have to calculate the time remaining till the next hour, so that every invocation is on the hour as you want.

If you want to start the action exactly at minute/second 0, then truncating to an hour is a bit tricky in Go.

And the time shifts a bit every time because all guarantees about timing has the condition at least (for example time.Sleep(d) sleeps at least for d duration of time; not exactly d).

With these requirements, this code does the task:

func main() {
    timer := time.NewTimer(nextDelay())
    for {
        <-timer.C
        timer.Reset(nextDelay())
        time.Sleep(time.Minute * 59) // the task, or you could do more tricks with channels
    }
}

func nextDelay() time.Duration {
    now := time.Now()
    return truncateHour(now).Add(time.Hour).Sub(now)
}

func truncateHour(t time.Time) time.Time {
    t = t.Truncate(time.Minute * 30)
    if t.Minute() > 0 {
        t = t.Add(time.Minute * -1).Truncate(time.Minute * 30)
    }
    return t
}