英文:
golang exception to rule "interface pointer can't implement interface"
问题
在golang.org的官方FAQ中,在指针部分有这样一句话:
“这个洞见是,尽管指向具体类型的指针可以满足一个接口,但指向接口的指针除了一个例外之外,永远不能满足一个接口。”
出于好奇,上述规则的例外是什么?
也就是说,什么情况下接口指针可以实现一个接口?
英文:
In golang.org's official FAQ, under pointers there is this quote:
"The insight is that although a pointer to a concrete type can satisfy an interface, with one exception a pointer to an interface can never satisfy an interface"
Out of curiosity, what is that exception to the above rule?
i.e when can an interface pointer implement an interface?
答案1
得分: 1
下面写着:
> 唯一的例外是,任何值,甚至是指向接口的指针,都可以赋值给空接口类型(interface{})的变量。即便如此,如果该值是指向接口的指针,几乎肯定是个错误;结果可能会令人困惑。
英文:
Just below it says:
> The one exception is that any value, even a pointer to an interface,
> can be assigned to a variable of empty interface type (interface{}).
> Even so, it's almost certainly a mistake if the value is a pointer to
> an interface; the result can be confusing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论