Golang : Schedule Cron Jobs with Crontab

This golang tutorial help to create schedule a jon to execute at specific time interval

By the way, I started to engage in golang recently... I remember that I used Golang to do something in LeTV, and I had to give up golang because of various problems.

There is a function that requires regular execution tasks, because it is coupled with the internals of the project, so it is not suitable for operation with linux crontab. Find a popular golang cron module in github that is easy to use! We only need to register a bunch of tasks to the scheduler. 
. . .

Getting started

To get the package, execute:
go get gopkg.in/robfig/cron.v2
To import this package, add the following line to your code:

import "gopkg.in/robfig/cron.v2"

Golang crontab project address:

Specific documentation:


CRON Expression Format

A cron expression represents a set of times, using 6 space-separated fields.
Field name Mandatory? Allowed values Allowed special characters
---------- ---------- -------------- --------------------------
Seconds No 0-59 * / , -
Minutes Yes 0-59 * / , -
Hours Yes 0-23 * / , -
Day of month Yes 1-31 * / , - L W
Month Yes 1-12 or JAN-DEC * / , -
Day of week Yes 0-6 or SUN-SAT * / , - L #
Year No 1970–2099 * / , -

Note: Month and Day-of-week field values are case insensitive. "SUN", "Sun", and "sun" are equally accepted.


Predefined schedules

You may use one of several pre-defined schedules in place of a cron expression.
Predefined schedules
You may use one of several pre-defined schedules in place of a cron expression.
Entry Description Equivalent to
@annually Run once a year at midnight in the morning of January 1 0 0 0 1 1 * *
@yearly Run once a year at midnight in the morning of January 1 0 0 0 1 1 * *
@monthly Run once a month at midnight in the morning of the first of the month 0 0 0 1 * * *
@weekly Run once a week at midnight in the morning of Sunday 0 0 0 * * 0 *
@daily Run once a day at midnight 0 0 0 * * * *
@hourly Run once an hour at the beginning of the hour

Never miss a post from Chris Gregori, when you sign up for Ednsquare.