1

Is theere a way to use a string as a function name and call it?

I have a map of strings to function names
stuff := map[string]string{'keyword','function'}

and when the keyword is used, i want to call function with 2 arguments
stuff['keyword'](arg1,arg2)

But it gives me this error:

cannot call non-function key (type string)

Is there a way to keep my string to string map and still achieve this?

2

1 Answer 1

1

The map you're using isn't syntactically valid. You probably want something like this:

stuff := map[string]func(string, string)

You would then be able to use your string key to pull out a function from the map and call it:

stuff["keyword"]("foo", "goo")

GoPlay:
https://play.golang.org/p/DNALJOmoiZ

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.