dongyuandou2521 2018-07-14 01:57
浏览 15
已采纳

Golang中值类型/指针类型的差异

type T struct {
    Name string
}
func (t T) M1 () {
    t.Name = "name1"
}
func (t *T) M2 () {
    t.Name = "name2"
}
type intf interface {
    M1()
    M2()
}
func main() {
    var t1 T = T{"t1"}
    t1.M1()
    t1.M2()
    var t2 intf = t1
    t2.M1()
    t2.M2()
}

Why when I usevar tf intf = &t1, It's correct But when I usevar tf intf = &t1, It's not correct Is there any difference in the two representations?

  • 写回答

1条回答 默认 最新

  • doupike2351 2018-07-14 02:57
    关注

    when I usevar tf intf = &t1, It's correct But when I usevar tf intf = &t1, it's not

    Since in your example you are using tf intf = t1 (and it does not compile), I think you probably meant:

    when I usevar tf intf = &t1, It's correct But when I usevar tf intf = t1, it's not

    So the question here is why &T{"t1"} (pointer) fulfils the interface intf while T{"t1"} (value) does not.

    In your example, the interface has two methods M1 and M2.

    Type T implements both methods BUT:

    • M1 has T value as the receiver
    • M2 has T pointer as the receiver

    So why does the compiler consider a T pointer to have both methods implemented but considers that a T value does not?

    Answer can be found here in the spec:

    https://golang.org/ref/spec#Method_sets

    The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T).

    The spec says that a T pointer has all methods defined with receivers T and *T (so both M1 and M2 in this case).

    But a value of T only has the methods defined with receiver T (so only M1 in this case).

    Since the interface has both methods, only the pointer can fulfil it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?