gpt4 book ai didi

go - 返回返回类型匹配接口(interface)的回调时出错

转载 作者:IT王子 更新时间:2023-10-29 02:18:10 26 4
gpt4 key购买 nike

错误示例@ play.golang.org:http://play.golang.org/p/GRoqRHnTj6

以下代码返回“prog.go:16: cannot use NewMyGame (type func() MyGame) as type func() Playable in return argument”,即使接口(interface)完全是空的。请在下面找到附带的代码,不幸的是我完全被难住了,非常感谢任何帮助。

package main

// Define an arbitrary game type
type MyGame struct{}

// Create a constructor function for arbitrary game type
func NewMyGame() MyGame {
return MyGame{}
}

// Define an interface defining game types
type Playable interface{}

// In my app it will return a list of constructors matching interface
func Playables() func() Playable {
return NewMyGame
}

func main() {}

最佳答案

正如错误所说,

cannot use NewMyGame (type func() MyGame) as type func() Playable

一个简单的修复是

func Playables() func() Playable {
return func() (Playable) {
return NewMyGame()
}
}

关于go - 返回返回类型匹配接口(interface)的回调时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17405303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com