Golang异常情况:接口指针无法实现接口规则。

huangapple go评论83阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2015年7月30日 02:06:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/31707969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定