返回返回类型匹配接口的回调时出错

错误示例@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”。也请找到下面附带的代码,不幸的是我完全被难住了,任何帮助将不胜感激。


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() {}


慕沐林林
浏览 172回答 1
1回答

UYOU

就像错误所说的那样,cannot use NewMyGame (type func() MyGame) as type func() Playable一个简单的解决方法是func Playables() func() Playable {    return func() (Playable) {        return NewMyGame()    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go