For example I have list of functions that I want to compare:
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))
}
}
What is the correct way to compare if two functions are the same?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…