我有一种休闲感:
func NewMethodDescriptor(typ interface{}) *MethodDescriptor {
reflectedMethod := reflect.ValueOf(typ)
methodType := reflectedMethod.Type
paramCount := methodType.NumIn() - 1
...但当我尝试时:
NewMethodDescriptor(func(){})我得到了这个编译时错误:
methodType.NumIn undefined (type func() reflect.Type has no field or method NumIn)发布于 2018-07-05 05:16:22
更具体地说:
reflectedMethod.Type返回函数,func (v Value) Type() TypereflectedMethod.Type()返回结果,表示v的类型。您可以在"Fun with the reflection package to analyse any function.“中看到更完整的示例。
func FuncAnalyse(m interface{}) {
//Reflection type of the underlying data of the interface
x := reflect.TypeOf(m)
numIn := x.NumIn() //Count inbound parameters
numOut := x.NumOut() //Count outbounding parameters
fmt.Println("Method:", x.String())
fmt.Println("Variadic:", x.IsVariadic()) // Used (<type> ...) ?
fmt.Println("Package:", x.PkgPath())
}https://stackoverflow.com/questions/32198387
复制相似问题