繁体   English   中英

检查结构是否实现了给定的接口

[英]Check if struct implements a given interface

我需要遍历结构类型的所有字段并检查它们是否实现了给定的接口。

type Model interface {...}

func HasModels(m Model) {
    s := reflect.ValueOf(m).Elem()
    t := s.Type()
    modelType := reflect.TypeOf((*Model)(nil)).Elem()

    for i := 0; i < s.NumField(); i++ {
        f := t.Field(i)
        fmt.Printf("%d: %s %s -> %s\n", i, f.Name, f.Type, f.Type.Implements(modelType)) 
    }       
}

然后,如果使用像这样的结构调用 HasModels:

type Company struct {...}

type User struct {
    ...
    Company Company
}

HasModels(&User{})

公司和用户都实现了模型; 我得到 f.Type.Implements(ModelType) 为 User 结构的 Company 字段返回 false。

这是出乎意料的,所以,我在这里做错了什么?

遗憾的是,遗漏了必要的部分(请总是发布完整的程序),所以我只能猜测问题出在指针接收器上定义的方法中,在这种情况下,代码的行为预期的。 检查此示例及其输出:

package main

import (
        "fmt"
        "reflect"
)

type Model interface {
        m()
}

func HasModels(m Model) {
        s := reflect.ValueOf(m).Elem()
        t := s.Type()
        modelType := reflect.TypeOf((*Model)(nil)).Elem()

        for i := 0; i < s.NumField(); i++ {
                f := t.Field(i)
                fmt.Printf("%d: %s %s -> %t\n", i, f.Name, f.Type, f.Type.Implements(modelType))
        }
}

type Company struct{}

func (Company) m() {}

type Department struct{}

func (*Department) m() {}

type User struct {
        CompanyA    Company
        CompanyB    *Company
        DepartmentA Department
        DepartmentB *Department
}

func (User) m() {}

func main() {
        HasModels(&User{})
}

操场


输出:

0: CompanyA main.Company -> true
1: CompanyB *main.Company -> true
2: DepartmentA main.Department -> false
3: DepartmentB *main.Department -> true

有一种更简单的方法可以做到这一点,不需要反思。 例如:

type middlewarer interface {Middleware() negroni.Handler}
for _, controller := range ctrls {
    if m, ok := interface{}(controller).(middlewarer); ok {
        n.Use(m.Middleware())
    }
}

仅在实现中间件接口的那些切片元素中调用Middleware()方法。

如果变量不是接口,则将其设为一个。

foo := interface{}(yourVar)

然后你可以检查它是否实现了你感兴趣的接口。

bar, ok := foo.(yourInterface)
if !ok {
    // handle the case where it doesn't match the interface
}
bar.methodOnYourInterface()

这是一个更加充实的例子的操场链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM