首页 > 解决方案 > 什么时候去反映CanInterface是假的?

问题描述

根据这个操场示例(https://play.golang.org/p/Jr64yE4zSpQ),以及CanInterfacein的实现reflect/value.go,看起来CanInterface只有私有字段才为假?

CanInterface当为假时还有哪些其他情况?

游乐场示例:

num := 6
meta := reflect.ValueOf(num)
fmt.Println("canInterface:", meta.CanInterface() == true)

meta = reflect.ValueOf(&num)
fmt.Println("canInterface:", meta.CanInterface() == true)

foo := Foo{}
meta = reflect.ValueOf(&foo)
fmt.Println("canInterface:", meta.CanInterface() == true)
meta = meta.Elem()
fmt.Println("canInterface:", meta.CanInterface() == true)
publicField := meta.FieldByName("Number")
privateField := meta.FieldByName("privateNumber")
fmt.Println(
    "canInterface:", 
    publicField.CanInterface() == true,
    // Woah, as per the implementation (reflect/value.go) 
    // this is the only time it can be false
    privateField.CanInterface() != true)

var fooPtr *Foo
var ptr anInterface = fooPtr
meta = reflect.ValueOf(ptr)
fmt.Println("canInterface:", meta.CanInterface() == true)

meta = reflect.ValueOf(&foo)
meta = meta.Elem() // ptr to actual value
publicField = meta.FieldByName("Number")
ptrToField := publicField.Addr()
fmt.Println("canInterface:", ptrToField.CanInterface() == true)

反映/value.go

func (v Value) CanInterface() bool {
if v.flag == 0 {
    panic(&ValueError{"reflect.Value.CanInterface", Invalid})
}
// I think "flagRO" means read-only?
return v.flag&flagRO == 0
}

标签: goreflection

解决方案


如果你深入研究 的源代码CanInterface,你可以看到这一行:

return v.flag&flagRO == 0

在它下面一点,来自函数的这段代码valueInterface

if safe && v.flag&flagRO != 0 {
    // Do not allow access to unexported values via Interface,
    // because they might be pointers that should not be
    // writable or methods or function that should not be callable.
    panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
}

由于v.flag&flagRO != 0相当于!CanInterface,我们可以从它下面的注释中得出结论,当是一个未导出的结构字段或方法时,它CanInterface是错误的。reflect.Value


推荐阅读