检查方法类型是否与函数类型匹配

给定以下示例,如何检查方法是否与函数签名匹配?


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

    }

}


https://play.golang.org/p/rIDfp0E14ge


幕布斯6054654
浏览 105回答 1
1回答

开心每一天1111

1.使用reflect.Value.Type().ConvertibleTo注意reflect.ValueOfinstedreflect.TypeOfpackage mainimport (&nbsp; &nbsp; "context"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "reflect")type Fn func(context.Context)type testStruct struct{}func (*testStruct) DoSomething(context.Context)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{}func (*testStruct) DoSomethingElse([]byte)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {}func (*testStruct) DoSomethingElse2(context.Context) error { return nil }func main() {&nbsp; &nbsp; structType := reflect.ValueOf(&testStruct{})&nbsp; &nbsp; for i := 0; i < structType.NumMethod(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("======================")&nbsp; &nbsp; &nbsp; &nbsp; method := structType.Method(i)&nbsp; &nbsp; &nbsp; &nbsp; // compare method and Fn&nbsp; &nbsp; &nbsp; &nbsp; if method.Type().ConvertibleTo(reflect.TypeOf((Fn)(nil))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("function of correct type")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}https://play.golang.org/p/A9_bpURinad2. 分别检查输入和输出package mainimport (&nbsp; &nbsp; "context"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "reflect")type Fn func(context.Context)type testStruct struct{}func (*testStruct) DoSomething(context.Context) {}func (*testStruct) DoSomethingElse([]byte)&nbsp; &nbsp; &nbsp; {}func main() {&nbsp; &nbsp; structType := reflect.TypeOf(&testStruct{})&nbsp; &nbsp; rctx := reflect.TypeOf(new(context.Context)).Elem()&nbsp; &nbsp; for i := 0; i < structType.NumMethod(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("======================")&nbsp; &nbsp; &nbsp; &nbsp; method := structType.Method(i)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(method.Name)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(method.Type.String())&nbsp; &nbsp; &nbsp; &nbsp; if method.Type.NumIn() != 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("wrong number of inputs, expected 1")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if method.Type.In(1) != rctx {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("input of wrong type, expected context.Context")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if method.Type.NumOut() != 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("wrong number of outputs, expected 0")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%v is a function of correct type\n", method.Name)&nbsp; &nbsp; }}https://play.golang.org/p/YDsJ9MSiumF
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go