Tuesday, July 15, 2025
HomeGolangRun perform as var/worth from DB - Getting Assist

Run perform as var/worth from DB – Getting Assist


Hello,
I’m new to Golang so sorry if I say or write one thing utterly off :slightly_smiling_face:

I’m attempting to run capabilities as jobs and I retailer perform names in DB after which attempt to run them as Cronjob, and it really works until level the place it must execute.

So I attempted to map them:

var funcs = map[string]func() {"countRegistrations":countRegistrations}

after which execute it with:

funcs["""+name+"""]()

The place title is retrieved from DB
however this throws reminiscence error panic.

I additionally figured that this isn’t versatile since I can add new job (func title in DB) however for the mapping I’ve to restart DB.

Is there a approach to name the perform which title is saved within the variable title?

Thanks

Hiya there. Are you certain you want these additional quotes in key? It’s all the time higher to all the time verify existence, one thing like

fn, okay := funcs[name]
if okay {
    fn()
}

Plus, you’ll be able to most likely add some job to replace funcs mapping to reload the map with new values from db.

Hey, thanks for suggestions, I feel i attempted with out quotes and it was nil, however it may be potential that it was nil reason behind db fetching issues i had earlier, I’ll attempt.

As for the job to replace map, certainly choice, i have already got it that’s fetching them from db, forgot that i can replace map additionally :wink:

Thanks quite a bit

Certainly it really works now with out quotes :slight_smile: good.

however i’m unsure how you can append it to the map, that is the way it appears to be like now:

var funcs = map[string]func() {"funcA":funcA}

so i’m noty certain how you can name append trigger of those quotes once more :slight_smile:

Are you attempting to append golang object (perform) into the map or is it already in db and also you simply use the title to name the item?

Map append works one thing like this:

m := make(map[string], 0)
m[key] = func

The thought was to have a job selecting up new information from DB, that don’t have jobId up to date, so funName discipline was simply string (varchar) title of the perform I need to run from that most important job (as one other job after which have that perform run as separate job). So it will permit me so as to add extra jobs dynamically.

I first tried to invoke funName similar to that however acquired:
./most important.go:137:4: funName (variable of sort string) isn't used
in order that lead me to that funcs map which have literal string mapped to func title, it really works now when I’ve hardcoded string and title in that map :slight_smile:

I feel you would want to make use of reflection to name your func through its title

See Go Playground – The Go Programming Language

Let me look it up the , thanks

Is there a approach to parametrise "hiya" additionally, try this i don’t need to hardcode key within the code, after which no must restart mail each time i add new report?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments