1

I hope to connect user input to functions. The user inputs are string. For example, "func_name=MyPrintf&s1=Hello, world\!" or "func_name=MyAdd&i1=1&i2=2"

Each function's code is,

func MyPrintf(s1 string) {
    fmt.Println(s1)
}

func MyAdd(i1, i2 int) {
    fmt.Println(i1, i2)
}

I want a map like below,

type Myfunc func(string) | func(int, int)   // <- Of course, it's wrong code, just I hope like this.

myMap := make(map[string]Myfunc)

myMap["MyPrintf"] = MyPrintf
myMap["MyAdd"] = MyAdd

myMap is possible to call function by func_name string from user input.

myMap[func_name](s1) Output: Hello, world! myMap[func_name](i1, i2) Output: 3

Is it possible? Maybe It's possible by using 'eval' I think, but I heard using 'eval' is not good. So, I thought using function pointer, however there's no function pointer in Golang.

I tried some generic programming for Golang,

type Myfunc interface {
    func(string) | func(int, int)
}

myMap := make(map[string]Myfunc)

Output: error occurred: cannot use type Myfunc outside a type constraint: interface contains type constraints

another try,

myMap := make(map[string]interface{})

myMap["MyPrintf"] = interface{}(MyPrintf)
myMap["MyPrintf"].(func(string))("Hello, world!")

Output: Hello, world!

myMap["MyAdd"] = interface{}(MyAdd)
myMap["MyAdd"].(func(int,int))(1, 2)

Output: 3

It works but must specify right func type, it's not comfortable. I think this way is not proper to my scenario. Please give me help. I'm sorry for my bad English writing.

2
  • I wonder why did you decide on this practice. why using a map for 2 different function with different arguments? Commented Jan 26, 2024 at 7:05
  • @Tomer S I want to implement code which run vary functions by input string. I considered again this problem, it is not good using map to implement, I think. Commented Jan 26, 2024 at 7:49

1 Answer 1

3

You can try this:

I must say it is not a good practice since panic \ error due to wrong types are not validated. I would consider another way to do so.

package main

import "fmt"

type GeneralFunc func(args ...interface{})

func main() {
    // Create a map of functions with the type GeneralFunc
    functionsMap := map[string]GeneralFunc{
        "MyPrintf": func(args ...interface{}) { fmt.Println(args[0].(string)) },
        "MyAdd":    func(args ...interface{}) { fmt.Println(args[0].(int), args[1].(int)) },
    }

    // Use the functions from the map
    functionsMap["MyPrintf"]("Hello World")
    functionsMap["MyAdd"](2, 3)
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. You're right. I think like you. This way not seems to be good.
I don't want to encourage the spread of empty interfaces, but for a tiny bit more type safety, you can do this: type GeneralFunc func(first any, rest ...any)
Also consider using a switch statement instead of a map.

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.