这个问题在这里已经有了答案:
Struct does not implement interface if it has a function which parameter implement interface
(2 个回答)
2年前关闭。
// 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")
}
我得到:
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
从它的外观来看,realY 实现了 myY,那为什么我不能这样做呢?这使得不可能干净地编写模拟单元测试。
最佳答案
不,它没有实现 myY
,正如错误明确指出的那样:
realX does not implement myX (wrong type for Do method)
have Do() realY
want Do() myY
方法签名必须与实现接口(interface)的类型完全匹配。方法签名不匹配 - 返回类型不同。没关系
realY
工具myY
;签名不一样。
关于testing - 转到类型错误 : struct does not implement interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48193800/