gpt4 book ai didi

pointers - 在什么意义上可以将 Go 的接口(interface)视为指针?

转载 作者:IT王子 更新时间:2023-10-29 01:39:42 24 4
gpt4 key购买 nike

this blog post作者指出:

Structs can implement interfaces, of course, so in general you tend to treat these as the same thing. But when you're dealing with a struct, you might be passing by reference, in which the type is *myStruct, or you might be passing by value, in which the type is just myStruct. If, on the other hand, the thing you're dealing with is "just" an interface, you never have a pointer to it -- an interface is a pointer in some sense. It can get confusing when you're looking at code that is passing things around without the * to remember that it might actually "be a pointer" if it's an interface rather than a struct.

在什么意义上可以将 Go 的接口(interface)视为指针?请提供一些示例。

最佳答案

这句话很奇怪。我想他想说的是:指针接收器上的方法清楚地表明该方法可能会修改接收器,因为该方法是在原始结构上调用的,而不是在副本上调用的。这清楚地表明“你在这里处理指针,这可能会修改原件!”一旦将结构包装在接口(interface)值中,就会丢失:您可以传递接口(interface)值的副本,但所有这些副本都包装相同的结构。

关于pointers - 在什么意义上可以将 Go 的接口(interface)视为指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27827500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com