Go 检查方法类型是否与函数类型匹配
给出以下示例,如何检查方法是否与函数签名匹配Go 检查方法类型是否与函数类型匹配,go,go-reflect,Go,Go Reflect,给出以下示例,如何检查方法是否与函数签名匹配 package main import ( "fmt" "context" "reflect" ) // signature to check type Fn func(context.Context) type testStruct struct {} func (*testStruct) DoSomething(context.Context){} func (*testStruct) DoSomethingEls
package main
import (
"fmt"
"context"
"reflect"
)
// signature to check
type Fn func(context.Context)
type testStruct struct {}
func (*testStruct) DoSomething(context.Context){}
func (*testStruct) DoSomethingElse([]byte){}
func main() {
structType := reflect.TypeOf(&testStruct{})
for i := 0; i < structType.NumMethod(); i++ {
fmt.Println("======================")
method := structType.Method(i)
fmt.Println(method.Name)
fmt.Println(method.Type.String())
// compare method and Fn signature
}
}
主程序包
进口(
“fmt”
“上下文”
“反映”
)
//签名核对
类型Fn func(context.context)
类型testStruct struct{}
func(*testStruct)DoSomething(context.context){}
func(*testStruct)DoSomethingElse([]字节){
func main(){
structType:=reflect.TypeOf(&testStruct{})
对于i:=0;i
1。使用reflect.Value.Type().ConvertibleTo
注意reflect.ValueOf
而不是reflect.TypeOf
package main
import (
"context"
"fmt"
"reflect"
)
type Fn func(context.Context)
type testStruct struct{}
func (*testStruct) DoSomething(context.Context) {}
func (*testStruct) DoSomethingElse([]byte) {}
func (*testStruct) DoSomethingElse2(context.Context) error { return nil }
func main() {
structType := reflect.ValueOf(&testStruct{})
for i := 0; i < structType.NumMethod(); i++ {
fmt.Println("======================")
method := structType.Method(i)
// compare method and Fn
if method.Type().ConvertibleTo(reflect.TypeOf((Fn)(nil))) {
fmt.Println("function of correct type")
}
}
}
主程序包
进口(
“上下文”
“fmt”
“反映”
)
类型Fn func(context.context)
类型testStruct struct{}
func(*testStruct)DoSomething(context.context){}
func(*testStruct)DoSomethingElse([]字节){
func(*testStruct)DoSomethingElse2(context.context)错误{return nil}
func main(){
structType:=reflect.ValueOf(&testStruct{})
对于i:=0;i
2.分别检查输入和输出
主程序包
进口(
“上下文”
“fmt”
“反映”
)
类型Fn func(context.context)
类型testStruct struct{}
func(*testStruct)DoSomething(context.context){}
func(*testStruct)DoSomethingElse([]字节){
func main(){
structType:=reflect.TypeOf(&testStruct{})
rctx:=reflect.TypeOf(new(context.context)).Elem()
对于i:=0;i
@mh cbon我做了,据我所知,我可以检查
testStruct
是否实现了一些接口。在本例中,我想检查struct方法是否具有签名func(context.context)
。我尝试了实现
,但没有成功。如果可以使用实现,请您将其放在回答中好吗?好的,我找到了解决方案。出于某种原因,如果浏览类型为reflect.TypeOf(&testStruct{})
的方法和值为reflect.ValueOf(&testStruct{})
,则会得到不同的结果。谢谢你。我已经用你的解决方案更新了答案。不客气,这不是一个容易处理的包。
package main
import (
"context"
"fmt"
"reflect"
)
type Fn func(context.Context)
type testStruct struct{}
func (*testStruct) DoSomething(context.Context) {}
func (*testStruct) DoSomethingElse([]byte) {}
func main() {
structType := reflect.TypeOf(&testStruct{})
rctx := reflect.TypeOf(new(context.Context)).Elem()
for i := 0; i < structType.NumMethod(); i++ {
fmt.Println("======================")
method := structType.Method(i)
fmt.Println(method.Name)
fmt.Println(method.Type.String())
if method.Type.NumIn() != 2 {
fmt.Println("wrong number of inputs, expected 1")
continue
}
if method.Type.In(1) != rctx {
fmt.Println("input of wrong type, expected context.Context")
continue
}
if method.Type.NumOut() != 0 {
fmt.Println("wrong number of outputs, expected 0")
continue
}
fmt.Printf("%v is a function of correct type\n", method.Name)
}
}