Skip to content

Commit 700550e

Browse files
Merge pull request #53 from Aashna-Agrawal/aashna
Cron Example
2 parents 15b47ce + 1a885cd commit 700550e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

advanced/cron.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/robfig/cron"
6+
"time"
7+
)
8+
9+
func main() {
10+
c := cron.New()
11+
MinuteCheck := time.Now().Minute() + 1
12+
SpecVal := fmt.Sprintf("%d", MinuteCheck) + " * * * *"
13+
fmt.Println("The SpecVal is ", SpecVal)
14+
_, err := c.AddFunc(SpecVal, PrintNumber)
15+
if err != nil {
16+
fmt.Println("Could not register cron - PrintNumber. Error:", err)
17+
}
18+
c.Start()
19+
time.Sleep(time.Second * 70)
20+
}
21+
22+
func PrintNumber() {
23+
fmt.Println("The function was triggered at time:", time.Now())
24+
}

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,13 @@ go run command_line_arguments.go
543543
go run command_line_arguments.go -env=qa -consumer=true
544544
```
545545

546+
Cron Golang Example
547+
We can trigger a function at a particular time through cron
548+
549+
```Shell
550+
go run cron.go
551+
```
552+
546553
## Compile
547554

548555
One great aspect of Golang is, that you can start go applications via ```go run name.go```, but also compile it to an executable with ```go build name.go```. After that you can start the compiled version which starts much faster.

0 commit comments

Comments
 (0)