小编Max*_*man的帖子

获取基于原始类型的类型的reflect.Kind

我想将reflect.Kind作为实现接口的类型的reflect.Interface,但其实现基于原始类型:type id string

对此问题的另一种答案可能是如何在调用 Kind() 时获取返回reflect.Interfaces 的任何类型的reflect.Type。

这是Go Playground上的完整示例:

type ID interface {
    myid()
}

type id string

func (id) myid() {}

func main() {
    id := ID(id("test"))
    
    fmt.Println(id)
    fmt.Println(reflect.TypeOf(id))
    
    // How to get the kind to return "reflect.Interface" from the var "id"?
    fmt.Println(reflect.TypeOf(id).Kind())
}
Run Code Online (Sandbox Code Playgroud)

reflection interface go

2
推荐指数
1
解决办法
2045
查看次数

标签 统计

go ×1

interface ×1

reflection ×1