2013-07-01 123 views
1

實施例時:http://play.golang.org/p/GRoqRHnTj6錯誤返回回調返回錯誤@ play.golang.org的類型匹配接口

以下代碼返回一個「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

這是完全一樣的錯誤說,

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

一個簡單的修正將是

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

謝謝您的回答,答案是有道理的,而且確實是在我的應用程序對我的作品。 您不會碰巧知道爲什麼在回調簽名級別無法實現會議接口要求,但在您將構造函數調用包裝在另一個回調中時工作? – beefsack

+1

@beefsack這是因爲Go中的類型非常具體。搜索有關接口片的問題。這也很重要,因爲接口基本上是一個2指針框,它改變了返回值的大小。 – cthom06