-1

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?

peterSO
  • 158,998
  • 31
  • 281
  • 276
snowpeak
  • 797
  • 9
  • 25
  • 2
    This is all wrong. There are two types and each has _one_ Abs() method and the "feature of the receiver" is how interfaces work. Nothing special here. – Volker Jul 02 '18 at 11:46
  • This is not a feature of receivers. Receivers are attached to methods and used when calling those methods upon receivers. – Himanshu Jul 02 '18 at 12:05
  • 2
    Thank you for answering. But one thing I doubt is the use of upvote and downvote, is stackoverflow a place to ask questions or a place to gather correct understanding of issues? hmmmm – snowpeak Jul 02 '18 at 12:16
  • 1
    Please check [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) – saddam Jul 02 '18 at 12:25
  • @peterSO Thanks for the information well I have not downvoted I have answered OP comment of `is stackoverflow a place to ask questions or a place to gather correct understanding of issue` – saddam Jul 02 '18 at 12:39
  • @ShiningGo I doubt *you* misunderstand how upvote/downvote works. I dare say there's something .. smelly .. in the kingdom of the Go tag. Far, far too many question downvotes, non-answers that quote specs instead of asnwering and comments that violate the [Be nice policy](https://stackoverflow.com/help/be-nice). *30 downvotes in a single day!!!!* – Panagiotis Kanavos Jul 03 '18 at 09:44

1 Answers1

2

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.

peterSO
  • 158,998
  • 31
  • 281
  • 276