I'm trying to create a map that will map strings to functions. Not all functions have the same signature. For example, I'd like to have something like this:
rf := map[string]func(...interface{}) error{
"FirstName": validateExistence(a.FirstName, "FirstName is required."),
"Postcode": validateMatchPattern(a.Postcode, `^\d{5}$`, "Could not match pattern for postcode."),
"Address": validateLength(a.Address, 0, 35, "Address must have up to 35 chars."),
}
I'm getting this error:
cannot use validateExistence("FirstName is required.") (type func(string) error) as type func(...interface {}) error in map value
If I change the map declaration to map[string]func(f string, m string) error
the error for the FirstName
is solved, but I get other errors for the other two functions:
cannot use validateMatchPattern(a.Postcode, "^\\d{5}$", "Could not match pattern for postcode.") (type error) as type func(string) error in map value
cannot use validateLength(a.Address, 0, 35, "Address must have up to 35 chars.") (type error) as type func(string) error in map value
I understand the issue is in the map declaration, in the func(...interface{})
to be more precise. This part must have the same signatures as the function I'm using as key.
So, my question is: is there any other way to declare the map that can hold functions with different signatures?