我想知道具体类型是否实现特定接口并将其打印出来。
我编写了一个示例 [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