go - 什么时候去反映CanInterface是假的?
问题描述
根据这个操场示例(https://play.golang.org/p/Jr64yE4zSpQ),以及CanInterface
in的实现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
}
解决方案
如果你深入研究 的源代码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
推荐阅读
- peoplesoft - AddDropDownItem 人员代码
- vb.net - 在 vb.net 上自学
- javafx - JAVAFX - 带有可修改自定义 TimePicker 单元格的 Tableview 关闭太快
- c# - 如何在不多次传递的情况下从 autofac 父范围解析?
- database - Laravel - 计算许多表中的记录
- xaml - 将 IMarkupExtension 与 StringFormat 一起使用
- javascript - JavaScript 获取 NaN
- sql-server - 从 SQL Server 连接到 MS Access 数据库的问题
- sql - 如何对列表的所有元素进行“和”运算符?
- python - 如何使用 Pandas 导入谷歌表格?