How to Execute a Go Function Daily at Noon While Handling Concurrent User Input?

Mary-Kate Olsen
Release: 2024-11-21 06:23:09
Original
999 people have browsed it

How to Execute a Go Function Daily at Noon While Handling Concurrent User Input?

Executing Code at Noon in Golang

In this scenario, we aim to run a specific function at noon every day while handling user input during the rest of the program's execution. Several approaches can effectively achieve this:

Interval Timer

Timer functions in Go allow us to execute tasks at predefined intervals. To schedule a function at noon daily, we can use:

  • timer.AfterFunc(): Executes a function after a specified duration.
  • time.Tick(): Returns a channel that sends the current time at the specified interval.
  • time.Sleep(): Pauses the program for a specified duration.
  • time.Ticker: Similar to timer.Tick(), but provides a channel that sends the current time at the specified interval and resets after each send.

Calculating the Interval

To determine the interval between the current time and the next noon, we first calculate the time remaining until the first noon after the program starts. We then use a 24-hour interval for subsequent noon tasks.

Sample Code using time.Sleep:

package main

import "fmt"
import "time"

func noonTask() {
    fmt.Println(time.Now())
    fmt.Println("do some job.")
}

func initNoon() {
    t := time.Now()
    n := time.Date(t.Year(), t.Month(), t.Day(), 12, 0, 0, 0, t.Location())
    d := n.Sub(t)
    if d < 0 {
        n = n.Add(24 * time.Hour)
        d = n.Sub(t)
    }
    for {
        time.Sleep(d)
        d = 24 * time.Hour
        noonTask()
    }
}

func main() {
    initNoon()
}
Copy after login

Using timer.AfterFunc:

package main

import (
    "fmt"
    "sync"
    "time"
)

func noonTask() {
    fmt.Println(time.Now())
    fmt.Println("do some job.")
    timer.AfterFunc(duration(), noonTask)
}

func main() {
    timer.AfterFunc(duration(), noonTask)
    wg.Add(1)
    // do normal task here
    wg.Wait()
}

func duration() time.Duration {
    t := time.Now()
    n := time.Date(t.Year(), t.Month(), t.Day(), 12, 0, 0, 0, t.Location())
    if t.After(n) {
        n = n.Add(24 * time.Hour)
    }
    d := n.Sub(t)
    return d
}

var wg sync.WaitGroup
Copy after login

Additional Information

  • [Github: Gocron](https://github.com/jasonlvhit/gocron) provides a utility library for scheduling jobs in Go.
  • [Golang - How to execute function at specific times](https://tutorialedge.net/golang/golang-execute-function-specific-times/)
  • [Golang: Implementing a cron / executing tasks at a specific time](https://blog.logrocket.com/how-to-implement-a-cron-in-golang/)

The above is the detailed content of How to Execute a Go Function Daily at Noon While Handling Concurrent User Input?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template