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.

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

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器