输入错误:结构未实现接口 [英] Go type-error: struct does not implement interface
本文介绍了输入错误:结构未实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// 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,为什么我不能这样做呢?这使得不可能干净地编写模拟单元测试.
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.
推荐答案
不,它没有实现myY
,因为错误明确指出:
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
用于实现接口的类型的方法签名必须完全匹配.方法签名不匹配-返回类型不同. realY
是否实现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.
这篇关于输入错误:结构未实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文