我有一个很好的函数,可以打印给定类型中给定方法的一些信息,这是一个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?")
}
}
喵喔喔
相关分类