Unable to get parameter names of a method or function.
The reason for this is that names are not very important for those who call a method or function. What matters is what types of parameters and their order.
A Function type denotes a set of all functions with the same parameter and result types. Type 2 functions that have the same parameter and result types are identical regardless of parameter names. The following code prints true :
func f1(a int) {} func f2(b int) {} fmt.Println(reflect.TypeOf(f1) == reflect.TypeOf(f2))
You can even create a function or method in which you do not even indicate the names of the parameters (in the list of parameters or results, the names must be present or all or none at all). This is a valid code:
func NamelessParams(int, string) { fmt.Println("NamelessParams called") }
If you want to create some kind of structure in which you call functions that pass values ββto "named" parameters (for example, by matching the incoming API parameters with the parameters of the Go / method function), you can use struct because with the help of the reflect package you can get named fields (e.g. Value.FieldByName() and Type.FieldByName() ), or you can use map . See This Related Question: Initialize Function Fields
The following is a relevant discussion of the golang-nuts mailing list .
source share