1

I am trying to schedule few jobs dynamically by reading configuration from file like below

import (
   "github.com/robfig/cron" 
   "fmt"
)
    masterJobDetails :=// this is array of job from file

    c := cron.New()
        for k, v := range masterJobDetails {

            fmt.Println(k, v.JobName)
            c.AddFunc(v.CronExpression, v.JobName)//JobName is function name in string format which need to call on specific interval

        }

        c.Start()

c.AddFunc() expect cron expression and func() as input,Can i get second argument using reflect with v.JobName string value

leaf bebop
  • 7,643
  • 2
  • 17
  • 27
Gan
  • 624
  • 1
  • 10
  • 31
  • 2
    You can't refer to functions given by their names. Possible duplicate of [Call all functions with special prefix or suffix in Golang](https://stackoverflow.com/questions/37384473/call-all-functions-with-special-prefix-or-suffix-in-golang/37384665#37384665). – icza Jan 19 '18 at 10:57

1 Answers1

0

Cannot cast string to func() type. Instead use switch or map to find the actual function.

for k, v := range masterJobDetails {
    var jobFunc func()
    switch v.JobName {
    case "job1":
        jobFunc = job1
    case "job2":
        jobFunc = job2      

    }
    c.AddFunc(v.CronExpression, jobFunc)
}
Ilayaraja
  • 2,388
  • 1
  • 9
  • 9