![](/img/trans.png)
[英]Why does GO compiler add a pointer value receiver method to the method set of non-pointer type when using the pointer Type Embedding?
[英]Does a `_` receiver on a value (non-pointer) still copy the value?
我的问题是,当调用方法时_
是否是接收者,是否产生了值的副本。
type Foo struct {
// Many fields, making a large struct
}
func (_ Foo) Test(v *T) int {
// Here we can't use the receiver but the method is still needed
}
所以我想知道在调用Test()
时Go实现是否仍然会复制Foo
值,即使实际上不可能改变接收器值。
var f Foo
f.Test() // Is this making a copy?
我也想知道指针的情况,默认情况下会自动解除引用。
var f = new(Foo)
f.Test() // Is this making a copy?
我试着看装配,我认为它可能正在制作副本,但我只是不知道足够确定。
有关情况的详细信息:
这是一个奇怪的情况,我不能使用指针。 代码是机器生成的,并且需要在v
参数上进行一些初始化时使类型满足接口。 (生成的代码包含有关在v
设置的Foo
元数据。)
因此,如果我将接收器设为指针,则“value”实例将不会满足接口。 对于每个实例,此方法将被调用一次,并且实例有时可能很大和/或大量创建,这就是为什么我要避免不必要的副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.