For a type to satisfy an interface, the type needs to implement the methods defined in the interface.
However, in the code snippet below, myStruct
does not have any methods written, but by using someInterface
as anonymous field, it satisfies someInterface
.
Can someone help explain why is it? Thank you.
package main
import "fmt"
type someInterface interface {
method1(int) string
method2(string) string
}
type myStruct struct {
someInterface
body int
}
func main() {
var foo someInterface
bar := myStruct{}
foo = bar // why no compile error??
fmt.Println(foo)
}