Not*_*otX 0 reflection methods go
我知道,有人问过类似的问题,但我没有找到这种情况的答案:
type ExportedStruct struct{ //comes from a dependency, so I can't change it
unexportedResource ExportedType
}
Run Code Online (Sandbox Code Playgroud)
我想打电话给一个出口的方法Close()上unexportedResource。
我所做的是:
rs := reflect.ValueOf(myExportedStructPtr).Elem() //myExportedStructPtr is a pointer to an ExportedStruct object
resourceField := rs.FieldByName("unexportedResource")
closeMethod := resourceField.MethodByName("Close")
closeMethod.Call([]reflect.Value{reflect.ValueOf(context.Background())})
Run Code Online (Sandbox Code Playgroud)
,这导致reflect.flag.mustBeExported using value obtained using unexported field。
这很烦人,因为我想运行多个利用 的测试ExportedStruct,但只要不使用底层资源,我就不能。
因为我可以访问私有字段(如解释在这里)我有一点希望,我被允许访问该场莫名其妙的公共方法,太。也许我只是反映错误?
| 归档时间: |
|
| 查看次数: |
507 次 |
| 最近记录: |