1

Here is the link to the code and description I was looking at: https://tour.golang.org/methods/11

I change method M of type *T to T, that is changing from a pointer receiver to a value receiver as below.

package main

import (
    "fmt"
    "math"
)

type I interface {
    M()
}

type T struct {
    S string
}

func (t T) M() {
    fmt.Println(t.S)
}

type F float64

func (f F) M() {
    fmt.Println(f)
}

func main() {
    var i I

    i = &T{"Hello"}
    describe(i)
    i.M()

    i = F(math.Pi)
    describe(i)
    i.M()
}

func describe(i I) {
    fmt.Printf("(%v, %T)\n", i, i)
}

However, the change above gave me the same result as it was still a pointer receiver.

(&{Hello}, *main.T)
Hello
(3.141592653589793, main.F)
3.141592653589793

I am not sure I got this concept right. From my understanding since interface variable i got assign a pointer to an instance of struct T, the type of that interface variable should be a pointer to struct T, and since pointer to struct T does not implement method M, it will cause a panic.

icza
  • 389,944
  • 63
  • 907
  • 827
user5574376
  • 371
  • 1
  • 5
  • 12

2 Answers2

1

Spec: Method sets:

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).

[...] 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.

So all methods you declare with value receiver will also belong to the method set of the corresponding pointer type, and thus all interfaces a non-pointer type implements will also be implemented by the pointer type too (and possibly more).

Community
  • 1
  • 1
icza
  • 389,944
  • 63
  • 907
  • 827
0

Go has some shortcuts. For example:

a.Method()
a.Field

is the same as

(*a).Method()
(*a).Field

is similar to the concept here https://tour.golang.org/moretypes/4

Johey
  • 275
  • 1
  • 8