比如我的,我想比較的功能列表:如何比較Go中的2個函數?
http://play.golang.org/p/_rCys6rynf
type Action func(foo string)
type Handler struct {
Get Action
Post Action
}
var routes map[string]Handler
func Undefined(foo string) {
}
func Defined(foo string) {
}
func init() {
routes = map[string]Handler{
`/`: Handler{Defined,Undefined},
}
}
func main() {
for _, handler := range routes {
if handler.Post != Undefined {
// do something
} // invalid operation: (func(string))(handler.Post) != Undefined (func can only be compared to nil)
if &handler.Post != &Undefined {
// do something
} // cannot take the address of Undefined
// invalid operation: &handler.Post != &Undefined (mismatched types *Action and *func(string))
}
}
什麼是比較,如果兩個功能是相同的正確方法?
第一個錯誤信息很清楚。 [函數只能與'nil'](https://golang.org/ref/spec#Comparison_operators)相比。 –
接口是否更清晰(並明顯解決問題)? –