3

According this question, golang will generate both type-receiver method and point-receiver method, which means the code below will work correctly and the value will change unexpectedly.

func (test *Test) modify() {
    test.a++
}

func main() {
    test := Test{10}
    fmt.Println(test)
    test.modify()
    fmt.Println(test)
}

I think it's acceptable to me. But when this mixes with interface, thing goes wrong.

type Modifiable interface {
    modify()
}

type Test struct {
    a int
}

func (test *Test) modify() {
    test.a++
}

func main() {
    test := Test{10}
    fmt.Println(test)
    test.modify()
    fmt.Println(test)

    var a Modifiable

    a = test
}

it said:

Test does not implement Modifiable (modify method has pointer receiver)

Why will this happen ?

And how golang actually handle method call ?

Community
  • 1
  • 1
KIDJourney
  • 1,200
  • 1
  • 9
  • 22

2 Answers2

1

if you wanted to use a method that has pointer receiver. It means you have to pass the address value.

here is the example :

package main

import "fmt"

type Modifiable interface {
    modify()
}

type Test struct {
    a int
}

func (test *Test) modify() {
    test.a++
}

func main() {
    test := Test{10}
    fmt.Println(test)
    test.modify()
    fmt.Println(test)

    var a Modifiable

    a = &test
    a.modify()
    fmt.Println(a)
}

In conclusion an interface will accept the address value whenever you create a pointer receiver in the method.

Gujarat Santana
  • 9,854
  • 17
  • 53
  • 75
1

When you said:

func (test *Test) modify() {
    test.a++
}

It means the interface Modifiable is implemented by the type *Test aka the Pointer to Test

Where as

func (test Test) modify() {
        test.a++
}

means that the interface is implemented by the type Test

Conclusion is: A type and a pointer to that type are 2 different types.

Ankur
  • 33,367
  • 2
  • 46
  • 72
  • But the `test` variable do have a method called `modify` and i can call by `test.modify()`, why it said `Test does not implement Modifiable`? – KIDJourney Jan 11 '17 at 05:13
  • Because you implemented the interface on `*Test` and not `Test`. The `modify` method is defined for `*Test` and not for `Test` type. When you are calling `test.modify` the compiler will automatically pass a `*Test` but from interface implementation point of view you need to explicitly use `*Test` – Ankur Jan 11 '17 at 05:23
  • So the answer in given link is wrong , the method for `Test` is not generated ? What compiler really do is pass the pointer to `pointer-method` instead of calling the non-exist generating method . – KIDJourney Jan 11 '17 at 07:07
  • 1
    Yes, no method is generated. Check out https://tour.golang.org/methods/6 and https://tour.golang.org/methods/7 – Ankur Jan 11 '17 at 07:10
  • Thanks a lot. @Ankur – KIDJourney Jan 11 '17 at 08:35