dousuitang5239 2015-02-10 13:30
浏览 11
已采纳

我如何获得方法参数名称

I have this nice function that prints some info about a given method in a given type, here is a goplay

it does all i need except i can't find a way to get the method argument names,any idea to achieve that?

    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?")

        }
}
  • 写回答

1条回答 默认 最新

  • duanlan7239 2015-02-10 13:58
    关注

    The names of local variables (including function arguments) are just for the programmer; the compiler discards them when it generates code. There is no way to retrieve argument names as they are not available in the compiled code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)