-4

I need to run my Co program continuously with five minute interval.

I tried using gocron but the program is not giving any output.

func hi() {
    fmt.Println("hi")
}
func main() {
    gocron.Every(5).Minute().Do(hi)
}

I expect this to run and print "hi" at every 5 min interval.

Soni Sol
  • 2,367
  • 3
  • 12
  • 23

1 Answers1

4

Your code is only setup a rule and immediately exits. You have to start the scheduler which will run the assigned jobs.

scheduler := gocron.NewScheduler(time.UTC)
scheduler.Every(5).Minute().Do(hi)
scheduler.StartBlocking()

This way the scheduler will block the program until its stopped (by Ctrl-C for example).

See the documentation for more info.

Peter
  • 29,454
  • 5
  • 48
  • 60
Fenistil
  • 3,734
  • 1
  • 27
  • 31