我有一个很好的功能,可以以给定类型打印有关给定方法的一些信息,这是一个goplay
除了我找不到找到方法参数名称的方法以外,我需要做的所有事情吗?
func AnalyzeMethod(instance interface {},methodName string){ m,ok := reflect.ValueOf(instance).Type().MethodByName(methodName) if ok{ isVariadic := m.Type.IsVariadic() var VariadicIndex int var VariadicType reflect.Type var FirstPositionalIndex int var LastPositionalIndex int var ArgsLength int = m.Type.NumIn()-1 //var []string ArgumentNames //can't find a way to get an ordered slice of names of every argument that the function takes if ArgsLength>0{ FirstPositionalIndex = 1 } if isVariadic{ VariadicIndex = ArgsLength VariadicType = m.Type.In(VariadicIndex) LastPositionalIndex = ArgsLength-1 } else { VariadicIndex = -1 LastPositionalIndex = ArgsLength } fmt.Println("ArgsLength:",ArgsLength) fmt.Println("FirstPositionalIndex:",FirstPositionalIndex) fmt.Println("LastPositionalIndex:",LastPositionalIndex) fmt.Println("Variadic:",isVariadic) fmt.Println("VariadicIndex:",VariadicIndex) fmt.Println("VariadicType:",VariadicType) fmt.Println("ArgumentNames","How?") } }
局部变量的名称(包括函数参数)仅适用于程序员。编译器在生成代码时将其丢弃。无法检索参数名称,因为它们在编译后的代码中不可用。