douningzhi1991 2016-07-03 05:07
浏览 92
已采纳

为什么不能将类型的值分配给实现带有接收者类型指针的方法的接口?

I am 2-days old in the world of Golang, and going through the go tour. I couldn't help but notice a peculiarity which I cannot seem to be able to come at terms with a proper reasoning.

This code is running perfectly:

package main
import (
    "fmt"
    "math"
)
type Vertex struct{
    X,Y float64
}
type Abser interface{
    Abs() float64
}
func (v Vertex) Abs() float64{ //method with value receiver argument
    return math.Sqrt(v.X*v.X+v.Y*v.Y)
}
func main(){
    var myVer Vertex = Vertex{3,4}
    var inter Abser
    inter = &myVer //assigning *Vertex type to inter
    fmt.Println(inter.Abs())
}

Meanwhile, the following code shows an error:

package main
import (
    "fmt"
    "math"
)
type Vertex struct{
    X,Y float64
}
type Abser interface{
    Abs() float64
}
func (v *Vertex) Abs() float64{ //method with pointer receiver argument
    return math.Sqrt(v.X*v.X+v.Y*v.Y)
}
func main(){
    var myVer Vertex = Vertex{3,4}
    var inter Abser
    inter = myVer //assigning Vertex type to inter
    fmt.Println(inter.Abs())
}

The error is:

interface.go:18: cannot use myVer (type Vertex) as type Abser in assignment: Vertex does not implement Abser (Abs method has pointer receiver)

Until reaching this section of the tour, I could understand that the creators of Go have let-go of the cumbersome notations like

(*v).method1name()

(&v).method2name()

So that methods with value receivers can be used with both values and pointers, and vice versa.

Why does the language discriminate between the two (value and pointer) when working with interfaces? Wouldn't it be more convenient if the same reference/dereference principles could apply here?

I hope that I am not missing something too apparent. Thanks!

  • 写回答

4条回答 默认 最新

  • dongmei1828 2016-07-03 06:00
    关注

    "Intro++ to Go Interfaces" illustrates the issue:

    *Vertex is a type. It’s the “pointer to a Vertex” type. It’s a distinct type from (non-pointer) Vertex. The part about it being a pointer is part of its type.

    You need consistency of type.

    "Methods, Interfaces and Embedded Types in Go":

    The rules for determining interface compliance are based on the receiver for those methods and how the interface call is being made.
    Here are the rules in the spec for how the compiler determines if the value or pointer for our type implements the interface:

    • The method set of the corresponding pointer type *T is the set of all methods with receiver *T or T

    This rule is stating that if the interface variable we are using to call a particular interface method contains a pointer, then methods with receivers based on both values and pointers will satisfy the interface.

    • The method set of any other type T consists of all methods with receiver type T.

    This rule is stating that if the interface variable we are using to call a particular interface method contains a value, then only methods with receivers based on values will satisfy the interface.

    Karrot Kake's answer about method set is detailed also in go wiki:

    The method set of an interface type is its interface.

    The concrete value stored in an interface is not addressable, in the same way that a map element is not addressable.
    Therefore, when you call a method on an interface, it must either have an identical receiver type or it must be directly discernible from the concrete type.

    Pointer- and value-receiver methods can be called with pointers and values respectively, as you would expect.
    Value-receiver methods can be called with pointer values because they can be dereferenced first.
    Pointer-receiver methods cannot be called with values, however, because the value stored inside an interface has no address.

    ("has no address" means actually that it is not addressable)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀