go - 具体类型与返回类型上的接口不匹配
问题描述
我在玩围棋,发现了一个我无法解决的问题。假设我有这样的代码:
// Imagine this is an external package for querying MySQL: I run a query
// and it gives me back a struct with a method "Result" to get the result
// as a string
// I can NOT modify this code, since it is an external package
package bar
type MySQL struct {}
func (m *MySQL) RunQuery() *MySQLResult {
return &MySQLResult{}
}
type MySQLResult struct {}
func (r *MySQLResult) Result() string {
return "foo"
}
我导入包并开始使用它:
// I created a little runner to help me
func run(m *bar.MySQL) string {
return m.RunQuery().Result()
}
func main() {
m := &bar.MySQL{}
fmt.Println(run(m)) // Prints "foo"
}
我真的很喜欢我的助手“run”,但我想让它更慷慨:我不希望人们总是给我一个 MySQL 客户端。它可以是任何具有“RunQuery”和“Result”方法的东西。所以我尝试使用接口:
type AnyDB interface {
RunQuery() interface{ Result() string }
}
func run(m AnyDB) string {
return m.RunQuery().Result()
}
可悲的是,这不再编译了。我收到此错误:
cannot use m (type *MySQL) as type AnyDB in argument to run:
*MySQL does not implement AnyDB (wrong type for RunQuery method)
have RunQuery() *MySQLResult
want RunQuery() interface { Result() string }
这是 Go 不支持的,还是我做错了什么?
解决方案
RunQuery
应该返回接口,否则你总是要处理强类型。
AnyDB
不是必需的,我添加它是为了方便。
AnyResult
应该在bar
包中定义或被导入其中。
type AnyDB interface {
RunQuery() AnyResult
}
type MySQL struct{}
func (m *MySQL) RunQuery() AnyResult {
return &MySQLResult{}
}
type AnyResult interface {
Result() string
}
type MySQLResult struct{}
func (r *MySQLResult) Result() string {
return "foo"
}
推荐阅读
- javascript - 如何让页面加载时调用的函数与按钮一起使用
- python - 根据折叠量计算标签分布
- amazon-web-services - 使用 boto3 在 EFS 中创建目录
- raspberry-pi - Raspberry Pi 4 上的 SDL2:SDL 初始化失败 没有可用的视频设备
- python - 将散点图合并为一个图形
- nested - Typo3 > 9.4 - Typoscript 中的嵌套条件可用?
- python - 在主 GUI 界面和顶级 GUI 界面之间进行通信
- python - 如何从 Scrapy 结果中获取字典
- android - 如何将虚拟相机发送到 Ubuntu 中的 Genymotion 或 Android Studio Emulator?
- javascript - 无法使用 typescript/javascript 遍历 JSON