无需编译即可检查变量实现接口

2024-03-13

我想知道具体类型是否实现特定接口并将其打印出来。 我编写了一个示例 [0],其中包含自定义结构(MyPoint),而不是接口类型。 MyPoint 具有 io.Reader 接口中定义的 Read 函数:

type MyPoint struct {
   X, Y int
}

func (pnt *MyPoint) Read(p []byte) (n int, err error) {
    return 42, nil
}

目的是获取具体类型 p 实现接口 io.Writer 的信息。 因此,我写了一个简短的主要 trieng 以获得正确的检查。

func main() {
     p := MyPoint{1, 2}
}

第一个想法是借助反射和类型开关来检查它并添加check(p)到主函数。

func checkType(tst interface{}) {
    switch tst.(type) {
    case nil:
        fmt.Printf("nil")
    case *io.Reader:
        fmt.Printf("p is of type io.Reader\n")
    case MyPoint:
        fmt.Printf("p is of type MyPoint\n")
    default:
        fmt.Println("p is unknown.")
    }
}

输出是:p is of type MyPoint。经过一番研究,我知道我应该预料到这一点,因为 Go 的类型是静态的,因此 p 的类型是 MyPoint 而不是 io.Reader。除此之外,io.Reader 是一个接口类型,与 MyPoint 类型不同。

我找到了一个解决方案,例如[1] 在编译时检查 MyPoint 是否可以是 io.Reader。有用。

var _ io.Reader = (*MyPoint)(nil)

但这不是我想要的解决方案。像下面这样的尝试也失败了。我想是因为上面的原因吧?

i := interface{}(new(MyPoint))
    if _, ok := i.(io.Reader); ok {
        fmt.Println("i is an io.Reader")
}
pType := reflect.TypeOf(p)
if _, ok := pType.(io.Reader); ok {
    fmt.Println("The type of p is compatible to io.Reader")
}

readerType := reflect.TypeOf((*io.Reader)(nil)).Elem()
fmt.Printf("p impl. Reader %t \n", pType.Implements(readerType))

是否存在一种无需编译即可检查 p 是否实现接口的解决方案?我希望有人能帮助我。

[0] http://play.golang.org/p/JCsFf7y74C http://play.golang.org/p/JCsFf7y74C(固定的)http://play.golang.org/p/cIStOOI84Y http://play.golang.org/p/cIStOOI84Y (old)

[1] 检查值是否实现接口的说明。戈兰 https://stackoverflow.com/questions/27803654/explanation-of-checking-if-value-implements-interface-golang


使用 Reflect 包完全可以做你想做的事情。这是一个例子:

package main

import (
    "fmt"
    "io"
    "reflect"
)

type Other int

type MyPoint struct {
    X, Y int
}

func (pnt *MyPoint) Read(p []byte) (n int, err error) {
    return 42, nil
}

func check(x interface{}) bool {
    // Declare a type object representing io.Reader
    reader := reflect.TypeOf((*io.Reader)(nil)).Elem()
    // Get a type object of the pointer on the object represented by the parameter
    // and see if it implements io.Reader
    return reflect.PtrTo(reflect.TypeOf(x)).Implements(reader)
}

func main() {

    x := MyPoint{0, 0}
    y := Other(1)

    fmt.Println(check(x)) // true
    fmt.Println(check(y)) // false
}

棘手的一点是要注意指针的处理方式。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无需编译即可检查变量实现接口 的相关文章

随机推荐