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.

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

报告相同问题?