有没有办法从另一个包访问结构的私有字段? [英] Is there any way to access private fields of a struct from another package?

查看:30
本文介绍了有没有办法从另一个包访问结构的私有字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个包含私有字段的包中有一个结构:

I have a struct in one package that has private fields:

package foo

type Foo struct {
    x int
    y *Foo
}

另一个包(例如,白盒测试包)需要访问它们:

And another package (for example, a white-box testing package) needs access to them:

package bar

import "../foo"

func change_foo(f *Foo) {
    f.y = nil
}

有没有办法将 bar 声明为一种朋友"包或任何其他方式能够访问 foo.Foo 的私有成员bar,但仍然为所有其他包保留它们的私有性(也许是 unsafe 中的某些内容)?

Is there a way to declare bar to be a sort of "friend" package or any other way to be able to access foo.Foo's private members from bar, but still keep them private for all other packages (perhaps something in unsafe)?

推荐答案

一种使用反射读取未导出成员的方法(在 Go <1.7 中)

There is a way to read unexported members using reflect (in Go < 1.7)

func read_foo(f *Foo) {
    v := reflect.ValueOf(*f)
    y := v.FieldByName("y")
    fmt.Println(y.Interface())
}

但是,尝试使用 y.Set 或以其他方式使用反射设置字段将导致代码恐慌,因为您试图在包外设置未导出的字段.

However, trying to use y.Set, or otherwise set the field with reflect will result in the code panicking that you're trying to set an unexported field outside the package.

简而言之:未导出的字段应该出于某种原因不导出,如果您需要更改它们,请将需要更改的内容放在同一个包中,或者公开/导出一些安全的方式来更改它.

In short: unexported fields should be unexported for a reason, if you need to alter them either put the thing that needs to alter it in the same package, or expose/export some safe way to alter it.

也就是说,为了完整回答问题,您可以这样做(并且必须在 Go >= 1.7 中这样做)

That said, in the interest of fully answering the question, you can do this (and have to do it this way in 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 中字段的顺序,更改它们的类型或大小,或者在预先存在的字段之前添加新字段,此函数将愉快地改变随机乱码数据的新表示,而无需告诉您.我也认为它可能会破坏这个块的垃圾收集.

This is a really, really bad idea. It's not portable, if int ever changes in size it will fail, if you ever rearrange the order of the fields in Foo, change their types, or their sizes, or add new fields before the pre-existing ones this function will merrily change the new representation to random gibberish data without telling you. I also think it might break garbage collection for this block.

请,如果您需要从包外部更改字段,请编写从包内更改它的功能或将其导出.

Please, if you need to alter a field from outside the package either write the functionality to change it from within the package or export it.

Edit2:既然您提到了白盒测试,请注意,如果您将目录中的文件命名为 <whatever>_test.go,它不会编译,除非您使用 go test,所以如果你想做白盒测试,在顶部声明 package <yourpackage> 这会让你访问未导出的字段,如果你想做黑盒测试,那么你使用 package _test.

Since you mention White Box testing, note that if you name a file in your directory <whatever>_test.go it won't compile unless you use go test, so if you want to do white box testing, at the top declare package <yourpackage> which will give you access to unexported fields, and if you want to do black box testing then you use package <yourpackage>_test.

但是,如果您需要同时对两个包进行白盒测试,我认为您可能会卡住,可能需要重新考虑您的设计.

If you need to white box test two packages at the same time, however, I think you may be stuck and may need to rethink your design.

这篇关于有没有办法从另一个包访问结构的私有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆