Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use go language timer Timer and Ticker

Shulou Source: shulou.com Published: 2022-05-31 22:13:42 10月05日 Update

This article editor for you a detailed introduction of "go language timer Timer and Ticker how to use", the content is detailed, the steps are clear, the details are handled properly, I hope that this "go language timer Timer and Ticker how to use" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Timer 1-"* / 5 *" package mainimport ("fmt"github.com/robfig/cron") / / main function func main () {cron2: = cron.New () / / create an instance of cron / / execute scheduled tasks (every 5 seconds) err:= cron2.AddFunc ("* / 5 *" Print5) if erratic query nil {fmt.Println (err)} / / start / close cron2.Start () defer cron2.Stop () select {/ / query statement Keep the program running This is equivalent to for {} / / execute function func print5 () {fmt.Println ("execute cron every 5s")} setting description ┌─ second range (0-60) │ ┌─ min (0-59) │ │ ┌─ hour (0) -23) │ ┌─ day of month (1-31) │ ┌─ month (1-12) │ ┌─ day of week (0-6) (0 to 6 are Sunday to │ │ Saturday) │ * *

Asterisk (*): indicates that the cron expression can match all values in this field. If you use an asterisk (month) in the fifth field, it indicates that each month

Slash (/): indicates the growth interval. For example, the value of the second field (minutes) is 3-59 minutes 15, indicating that execution starts at the third minute of each hour, and then every 15 minutes (i.e., 3 (3), 18 (3), 33 (3), 48 (3)). Here, it can also be expressed as: 315 jump.

Comma (,): used to enumerate values, for example, the sixth field value is MON,WED,FRI, indicating execution on Monday, Wednesday and Friday

Hyphen (-): indicates a range, for example, a value of 9-17 in the third field indicates 9am to 5pm directly per hour (including 9 and 17)

Question mark (?): used only for Day of month and Day of week, indicating that no value is specified and can be used instead of *

Timer 2-Timer-TickerTimer / / time to generate an event Ticker / / time to cycle to generate an event Timer- only executes package mainimport ("fmt"time") func main () {/ / create a timer, setting the time to 2s after 2s Write content to time channel (current time) timer: = time.NewTimer (2 * time.Second) fmt.Println ("current time:", time.Now ()) / / 2s, then write data to timer.C. After you have the data, you can read t: =

Tags: Time timer field content language event arrival data article function hour asterisk week range loop block proper one sentence task function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Shulou Information Shulou Tech Info Redmi