douhang1913 2018-07-02 11:36
浏览 21
已采纳

接收方是否确定要采用哪种方法?

A Tour of Go: https://tour.golang.org/methods/9

package main

import (
    "fmt"
    "math"
)

type Abser interface {
    Abs() float64
}

func main() {
    var a Abser
    f := MyFloat(-math.Sqrt2)
    v := Vertex{3, 4}

    a = f  // a MyFloat implements Abser
    a = &v // a *Vertex implements Abser

    // In the following line, v is a Vertex (not *Vertex)
    // and does NOT implement Abser.
    a = v

    fmt.Println(a.Abs())
}

type MyFloat float64

func (f MyFloat) Abs() float64 {
    if f < 0 {
        return float64(-f)
    }
    return float64(f)
}

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

In this exercise, there are two Abs() methods. But it seems that line 24, fmt.Println(a.Abs()), automatically applies the one that has a receiver with the same type as the variable.

Is this a feature of receivers?

  • 写回答

1条回答 默认 最新

  • duancuan6466 2018-07-02 12:33
    关注

    The Go Programming Language Specification

    Method sets

    A type may have a method set associated with it. The method set of an interface type is its interface. 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). Further rules apply to structs containing embedded fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.

    The method set of a type determines the interfaces that the type implements and the methods that can be called using a receiver of that type.


    The method set of a type determines the interfaces that the type implements and the methods that can be called using a receiver of that type.

    For example, simplifying the Go Tour example,

    package main
    
    import (
        "fmt"
        "math"
    )
    
    type Abser interface {
        Abs() float64
    }
    
    type Vertex struct {
        X, Y float64
    }
    
    func (v *Vertex) Abs() float64 {
        return math.Sqrt(v.X*v.X + v.Y*v.Y)
    }
    
    func main() {
        var a Abser
        a = &Vertex{3, 4} // a *Vertex implements Abser
        fmt.Println(a.Abs())
    }
    

    Playground: https://play.golang.org/p/cf3WMcBI0WJ

    Output:

    5
    

    Variable a of type Abser can contain any variable type that has the Abser method set : Abs() float64. Variable a contains a *Vertex which satisfies Abser with method set func (v *Vertex) Abs() float64. The expression a.Abs() executes the method Abs() for the type *Vertex that it currently contains.

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?