Go map of functions -
i have go program has function defined. have map should have key each function. how can that?
i have tried this, doesn't work.
func a(param string) { } m := map[string] func { 'a_func': a, } key, value := range m { if key == 'a_func' { value(param) } }
are trying this? i've revised example use varying types , numbers of function parameters.
package main import "fmt" func f(p string) { fmt.println("function f parameter:", p) } func g(p string, q int) { fmt.println("function g parameters:", p, q) } func main() { m := map[string]interface{}{ "f": f, "g": g, } k, v := range m { switch k { case "f": v.(func(string))("astring") case "g": v.(func(string, int))("astring", 42) } } }
Comments
Post a Comment