In Go, you can define custom functions and assign them as values to keys in a map. This allows for dynamic execution of functions based on a given key.
To achieve this, you can create a map of type map[string]func(...) where the key is a string and the value is a function. However, the provided code attempts to use a syntax that is not supported in Go.
To fix this, use the following code:
func a(param string) { fmt.Println("Function a called with param:", param) } func main() { m := map[string]func(string) { "a_func": a, } for key, value := range m { if key == "a_func" { value("test") } } }
In this example:
Note that the interface{} type in the provided code example is not necessary and can be simplified to func(...).
The above is the detailed content of How Can I Assign and Call Functions as Values in a Go Map?. For more information, please follow other related articles on the PHP Chinese website!