首页 文章

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

提问于
浏览
1

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

下面的代码返回“prog.go:16:不能使用NewMyGame(类型为func()MyGame)作为类型func()可以在返回参数中播放”即使接口完全为空 . 请找下面附带的代码,不幸的是我完全难过,任何帮助都会非常感激 .

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

1 回答

  • 1

    正如错误所说的那样,

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

    一个简单的解决方案就是

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

相关问题