英文:
Why is my interface containing a pointer not updating after the pointer is updated?
问题
我遇到的问题有很多限制,所以我将其大大简化为包含以下内容的不自然代码:
- 一个名为
Rect
的结构体 rA
和rB
都指向一个新的Rect
对象r1
和r2
,它们是包含rA
和rB
的接口r1Adr
和r2Adr
,它们是包含rA
和rB
的地址的接口
我想要执行rA = rB
,但在声明变量后不允许引用rA
或rB
。
(注意:你可能会认为这很容易,你只需执行*r1.(*Rect) = *r2.(*Rect)
。然而,这种方法的问题在于它不会将rA
指向rB
的Rect对象,它只是将rB
的Rect值克隆到rA
的Rect中。因此,我必须解引用指向rA
(r1Adr
)的指针,并将其赋值给rB
,以使rA
指向与rB
相同的Rect对象。)
以下是我尝试的方法:
type Rect struct {
width int
}
func main() {
rA := new(Rect)
rB := new(Rect)
var r1, r2, r1Adr, r2Adr any
r1 = rA
r2 = rB
r1Adr = &rA
r2Adr = &rB
*r1Adr.(**Rect) = *r2Adr.(**Rect)
// 打印存储的每个Rect对象的地址
fmt.Printf("%p, %p, %p, %p, %p, %p", rA, rB, r1, r2, *r1Adr.(**Rect), *r2Adr.(**Rect))
}
0xc00001c038, 0xc00001c038, 0xc00001c030, 0xc00001c038, 0xc00001c038, 0xc00001c038
PLAYGROUND: https://go.dev/play/p/VnQwx6V7DNa
从输出中可以看出,rA
正确更新为rB
,而r1Adr
和r2Adr
完全反映了这一点。然而,r1
的rA
没有更新为rB
,这可以通过其输出的不同来证明。为什么会这样?如果rA
被更新为rB
,并且如果r1
存储了rA
,那么为什么r1
指向的rA
没有更新为rB
?
如果能在不改变r1
为接口的情况下,以这种方式初始化r1
,使其在执行*r1Adr.(**Rect) = *r2Adr.(**Rect)
后正确更新,那将非常感激。
英文:
I have a lot of constraints in the issue I am encountering, so I have greatly simplified it to this unnatural code containing the following stuff:
- A struct named
Rect
rA
andrB
both of which point to a newRect
objectr1
andr2
, which areinterfaces
containingrA
andrB
r1Adr
andr2Adr
, which areinterfaces
containing the addresses ofrA
andrB
I want to do rA = rB
without being allowed to reference rA
or rB
after declaring my variables.
(Note: you would think it would be easy, and that you would just do *r1.(*Rect) = *r2.(*Rect)
. However, the issue with this approach is that it does not point rA
to rB
's Rect object—it merely clones the values of rB
's Rect to rA
's Rect. So, I instead have to dereference a pointer to rA
(r1Adr
) and assign it to rB
so that rA
points to the same Rect object as rB
.)
Below is my attempt at doing so:
type Rect struct {
width int
}
func main() {
rA := new(Rect)
rB := new(Rect)
var r1, r2, r1Adr, r2Adr any
r1 = rA
r2 = rB
r1Adr = &rA
r2Adr = &rB
*r1Adr.(**Rect) = *r2Adr.(**Rect)
// print the address of each Rect object being stored
fmt.Printf("%p, %p, %p, %p, %p, %p", rA, rB, r1, r2, *r1Adr.(**Rect), *r2Adr.(**Rect))
}
0xc00001c038, 0xc00001c038, 0xc00001c030, 0xc00001c038, 0xc00001c038, 0xc00001c038
PLAYGROUND: https://go.dev/play/p/VnQwx6V7DNa
As you can see from the output, rA
is correctly updating to rB
, and r1Adr
and r2Adr
are reflecting this perfectly. However, r1
's rA
is not updating to rB
, which we know because its output is different. Why is this? If rA
is being updated to rB
, and if r1
stores rA
, then how is it that the rA
pointed to by r1
is not updating to rB
?
Any insight as to how I can initialize r1
in such a way (without changing it from an interface) that it properly updates after doing *r1Adr.(**Rect) = *r2Adr.(**Rect)
would be greatly appreciated.
答案1
得分: 1
rA
和rB
是指向不同对象A
和B
的两个指针。
r1
是指向A
的接口,r2
是指向B
的接口。
r1Addr
是指向&rA
的接口,r2Addr
是指向&rB
的接口。
因此,当你更新r1Addr
的内容时,你将rA
指向rB
。
然而,r1
和r2
仍然指向A
和B
,它们没有改变。
英文:
rA
and rB
are two pointers pointing to separate objects, A
and B
.
r1
is an interface pointing to A
, and r2
is an interface pointing to B
.
r1Addr
is an interface pointing to &rA
, and r2Addr
is an interface pointing to &rB
.
Thus, when you update the contents of r1Addr1
, you point rA
to rB
.
However, r1
and r2
are still pointing to A
and B
; they did not change.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论