关于如何通过反射获取函数名称

 

反射的包名:reflect

步骤:

1:根据传入函数获取函数的指针

2:通过指针获取函数名称

代码:

func apply(op func(int , int) int , a,b int) int{
    pointer:= reflect.ValueOf(op).Pointer()
    funcName := runtime.FuncForPC(pointer).Name()
    fmt.Printf("the runing  func name is %s  , args is (%d , %d)" , funcName , a , b)
    return op(a , b)
}

func multiplication(a,b int)int{
    return  a*b
}
func main() {
    fmt.Println(apply(multiplication , 3, 4))
}

 

相关文章:

  • 2021-04-26
  • 2021-11-25
  • 2021-11-26
  • 2022-12-23
  • 2021-10-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-30
  • 2021-10-16
  • 2021-09-16
相关资源
相似解决方案