// BEGIN: external library
type realX struct {}
type realY struct {}
func (realX) Do() realY {
return realY{}
}
// END
type A struct {
a myX
}
type myY interface {}
type myX interface {
Do() myY
}
func foo (arg1 myY) {
}
func main() {
foo(realY{})
x := A{realX{}}
fmt.Println("Hello, playground")
}
I get:
cannot use realX literal (type realX) as type myX in field value:
realX does not implement myX (wrong type for Do method)
have Do() realY
want Do() myY
From the looks of it, realY implements myY, so why can't I do this? This is making it impossible to write mock unit tests cleanly.
No, it doesn't implement myY
, as the error clearly states:
realX does not implement myX (wrong type for Do method)
have Do() realY
want Do() myY
The method signature must match exactly for the type to implement the interface. The method signatures do not match - the return types are different. It doesn't matter if realY
implements myY
; the signatures are not the same.