有is种方法可以使用反射(在Go<;1.7中)处理read个未导出的成员
func read_foo(f *Foo) {
v := reflect.ValueOf(*f)
y := v.FieldByName("y")
fmt.Println(y.Interface())
}
但是,try 使用y.Set,或以其他方式使用reflect设置字段将导致代码panic ,因为您试图在包外设置未报告的字段.
简而言之:未导出的字段应该出于某种原因取消导出,如果您需要更改它们,可以将需要更改的内容放在同一个包中,或者公开/导出某种安全的方式来更改它.
也就是说,为了完全回答这个问题,你们can人这样做(并且在go>;=1.7中必须这样做)
func change_foo(f *Foo) {
// Since structs are organized in memory order, we can advance the pointer
// by field size until we're at the desired member. For y, we advance by 8
// since it's the size of an int on a 64-bit machine and the int "x" is first
// in the representation of Foo.
//
// If you wanted to alter x, you wouldn't advance the pointer at all, and simply
// would need to convert ptrTof to the type (*int)
ptrTof := unsafe.Pointer(f)
ptrTof = unsafe.Pointer(uintptr(ptrTof) + uintptr(8)) // Or 4, if this is 32-bit
ptrToy := (**Foo)(ptrTof)
*ptrToy = nil // or *ptrToy = &Foo{} or whatever you want
}
这是个非常非常糟糕的主意.它是不可移植的,如果int的大小发生变化,它就会失败,如果你在Foo中重新排列字段的顺序,改变它们的类型或大小,或者在已有字段之前添加新字段,这个函数会在不告诉你的情况下愉快地将新的表示形式更改为随机乱码数据.我还认为这可能会 destruct 这个街区的垃圾收集.
请注意,如果您需要从包外部更改字段,可以编写功能从包内更改它,也可以将其导出.
编辑2:由于您提到了白盒测试,请注意,如果您将目录<whatever>_test.go
中的一个文件命名为<whatever>_test.go
,除非您使用go test
,否则它不会编译,所以如果您想要进行白盒测试,请在顶部声明package <yourpackage>
,它将允许您访问未导出的字段,如果您想要进行黑盒测试,那么您可以使用package <yourpackage>_test
.
但是,如果您需要同时对两个软件包进行白盒测试,我认为您可能会被卡住,需要重新考虑您的设计.