SoFunction
Updated on 2025-03-05

Basic tutorial on using timer cron in Go language

What is cron

cron means: plan tasks, to put it bluntly, it is a scheduled task. I made an appointment with the system. It’s that simple.

Preface

cron is a timed job library developed by robfig. robfig always thinks earlier than others and gives us a lot of things that Gopher needs urgently. I think the emergence of revel back then was the same. Looking at the use of cron, it is still as concise and clear as ever. I found that in the world of Go, some products still have distinct personal characteristics, that is, the so-called personal charm, right? !

In short, the products developed by robfig are all advanced, more theoretically based, and very simple to use.

Let's see how to use cron:

c := ()
("0 30 * * * *", func() { ("Every hour on the half hour") })
("@hourly", func() { ("Every hour") })
("@every 1h30m", func() { ("Every hour thirty") })
()

First instantiate a cron object c, then call the AddFunc function of instance c. The AddFunc function receives a timed expression and a normal function, and finally start executing the instance and the timing job is fine.

The first ordinary function received is our business stuff. For example, if we want to print a Hello World! line string, we write a function like this:

func PrintSomeWord(){
("Hello World!")
}

PrintSomeWord is written, and I want to print it once a second, and cron comes in handy:

c := ()
("@every 1s",PrintSomeWord)
()

In this way, ordinary functions are determined by our business, so let’s not talk about them for now, and timed expressions are what we want to learn. From the name of cron, it is easy to associate it with cron timers in Linux system. Do they have a certain relationship? Judging from the usage below, the answer is yes. If you look closely at their timing expressions, it is very similar, which is easy to deal with, greatly lowering the threshold for using cron. After all, cron for Linux comes from a standard.

Let's take a look at the normal expression of cron:

("@every 1h30m", func() { ("Every hour thirty") })
("@daily", func() { ("Every day") })
("@hourly", func() { ("Every hour") })

The timing expressions of these three lines of code respectively represent: they are executed every 1 hour and 30 minutes, once a day, and once an hour.

@every is very wide. Modifying the suffix can represent different uses, such as seconds, hours, and days. Look at the code:

@every 1s
@every 1m
@every 1h
@every 1d

How is it very regular?

To go deeper, it may be that you dislike @every is not concise enough, such as:

@every 1h
@every 1d
@every 30d

Hourly, daily, monthly, and no suffix is ​​available:

@hourly
@daily
@monthly

Of course, simplicity is good, but there are often some situations where simplicity cannot be concise, such as mentioned above:

@every 1h30m

It's not possible to not use the suffix, right?

The hardest thing to understand is probably the following expression, because looking at this expression is like looking at a bunch of regular expressions, which makes ordinary people confused:

("0 30 * * * *", func() { ("Every hour on the half hour") })

Timed expression 0 30 * * * What does it mean? The first impression is that there is no regularity and I don’t know what it means. If you translate the foreign text Every hour on the half hour, it’s a bit of a concept, which means “execute once every hour and a half (1.5 hours)”! ! !

Well, when you see 0 30 and six placeholders, let’s reason: Is it second minute time day month year? I seem to have eyebrows, and I also want to know the specific way to use this expression. I can only talk about it after studying it carefully next time.

Use of Go timer cron expressions

If you want to use timing more flexibly, it may involve more complex methods of using six-bit expressions, such as execution every half hour:

0 30 * * * *

A six-digit expression looks a bit confusing, just like a regular expression, we need to understand it in steps;

Understand the meaning of six scopes

0 0 0 1 1 *
0 0 0 1 * *
0 0 0 * * 0
0 0 0 * * *
0 0 * * * *

As mentioned above, all six-digit placeholders can be numbers and characters, where characters are specific characters including: [* / , - ? JAN-DEC SUN-SAT]

Let's call it six scopes, in the order from left to right, which means second domain (1) minute domain (2) hour domain (3) date domain (4) month domain (5) week domain (6)

Month domains can be used. JAN-DEC and weekly domains can be used SUN-SAT, representing the English abbreviation of January-December and the English abbreviation of Monday to Sunday respectively. If you do not understand the English abbreviation of month and week, it is recommended to use numbers, because this requires memory and case-sensitiveness and is prone to errors.

The following list is convenient for understanding:

English abbreviation for January to December: Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec

Mon Tues Wed Thurs Fri Sat Sun

There are six scopes that can also be used for specific characters:

Specific character asterisk (*)

An asterisk means matching all reasonable values ​​and any domain can exist. For example, if the position of the month field (5) is used to use an asterisk*, which means that it is executed every month; similarly, if the position of the week field (6) is used to use an asterisk, which means that it is executed every week; other placeholders use it and so on.

Specific character scribing (/)

Slashing is usually used for a range increment and can exist in any domain. For example, use 3-59/15 to represent the time range of the 3rd minute to 59 minutes in the minute field (2), and execute every 15 minutes.

Specific character comma (,)

Commas are used to separate multiple points and can exist in any domain. For example, when using MON, WED, and FRI at the location of the week field (6), it means that the three points on Monday, Wednesday, and Friday meet the conditions.

Horizontal bars in specific characters (-)

Medium bars are usually used to define ranges and can exist in any domain. For example, the location of the hour domain (3) uses 9-17 to represent the time period range of the start 9 o'clock to the end 17 o'clock.

Specific character question mark (?)

A question mark means uncertainty in the meaning of a point. It can only be used in the date field (4) and the week field (6), indicating the specified value uncertainty. Only a question mark can be used once in an expression? If it is used in the date field (4), it cannot be used in the week field (6). It is different from an asterisk. An asterisk means matching all points, that is, it is OK for any day in a month and any week. The question mark means uncertainty in a month or uncertainty in a week. This is difficult to understand. It is necessary to explain some specific situations. For example, the 18th day of February happens to be the second day of the third week. So if 18 is defined in the date field (4) and 2 of the week field (6) defines overlap, this cron is not allowed.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.