I am asserting that the type of a pointer to a struct is implementing an interface in golang and there is something I don't understand in the code sample below:
package main
import (
"fmt"
)
type MyStruct struct {
Name string
}
func (m *MyStruct) MyFunc() {
m.Name = "bar"
}
type MyInterface interface {
MyFunc()
}
func main() {
x := &MyStruct{
Name: "foo",
}
var y interface{}
y = x
_, ok := y.(MyInterface)
if !ok {
fmt.Println("Not MyInterface")
} else {
fmt.Println("It is MyInterface")
}
}
I was expecting to do _, ok := y.(*MyInterface) since y is a pointer to MyStruct. Why can't I assert it is a pointer?