Golang方法接收器

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 ?

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.

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.