如何比较 Go 中的 2 个函数?
How to compare 2 functions in Go?
例如,我有要比较的函数列表:
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))
}
}
如果两个函数相同,比较的正确方法是什么?
没关系,找到答案了:
runtime.FuncForPC(reflect.ValueOf(handler.Post).Pointer()).Name() !=
runtime.FuncForPC(reflect.ValueOf(Undefined).Pointer()).Name()
在进一步讨论之前:您应该重构而不是比较函数值地址。
Slice, map, and function values are not comparable. However, as a special case, a slice, map, or function value may be compared to the predeclared identifier nil
.
函数值不可比较。你可以做的是比较函数值的地址是否相同(不是保存函数值的变量地址,而是函数值本身)。
You can't take the address of a function, but if you print it with the fmt
package, it prints its address. So you can use fmt.Sprintf()
获取函数值的地址。
查看此示例(基于您的代码):
hand := &Handler{Undefined, Defined}
p1 := fmt.Sprintf("%v", Undefined)
p2 := fmt.Sprintf("%v", hand.Get)
fmt.Println("Expecting true:", p1 == p2)
fmt.Println("Expecting false:", fmt.Sprintf("%v", Defined) == fmt.Sprintf("%v", hand.Get))
fmt.Println("Expecting true:", fmt.Sprintf("%v", Defined) == fmt.Sprintf("%v", hand.Post))
输出(在 Go Playground 上尝试):
Expecting true: true
Expecting false: false
Expecting true: true
另一种选择是使用 reflect.Value.Pointer()
来获取函数值的地址,这正是 fmt
包所做的:fmt/print.go
:
func (p *pp) fmtPointer(value reflect.Value, verb rune) {
// ...
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice,
reflect.UnsafePointer:
u = value.Pointer()
// ...
}
但是你应该重构而不是比较函数值地址。
你无法比较功能。它需要存储在一个变量中,并将其作为指针引用。
例如,我有要比较的函数列表:
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))
}
}
如果两个函数相同,比较的正确方法是什么?
没关系,找到答案了:
runtime.FuncForPC(reflect.ValueOf(handler.Post).Pointer()).Name() !=
runtime.FuncForPC(reflect.ValueOf(Undefined).Pointer()).Name()
在进一步讨论之前:您应该重构而不是比较函数值地址。
Slice, map, and function values are not comparable. However, as a special case, a slice, map, or function value may be compared to the predeclared identifier
nil
.
函数值不可比较。你可以做的是比较函数值的地址是否相同(不是保存函数值的变量地址,而是函数值本身)。
You can't take the address of a function, but if you print it with the fmt
package, it prints its address. So you can use fmt.Sprintf()
获取函数值的地址。
查看此示例(基于您的代码):
hand := &Handler{Undefined, Defined}
p1 := fmt.Sprintf("%v", Undefined)
p2 := fmt.Sprintf("%v", hand.Get)
fmt.Println("Expecting true:", p1 == p2)
fmt.Println("Expecting false:", fmt.Sprintf("%v", Defined) == fmt.Sprintf("%v", hand.Get))
fmt.Println("Expecting true:", fmt.Sprintf("%v", Defined) == fmt.Sprintf("%v", hand.Post))
输出(在 Go Playground 上尝试):
Expecting true: true
Expecting false: false
Expecting true: true
另一种选择是使用 reflect.Value.Pointer()
来获取函数值的地址,这正是 fmt
包所做的:fmt/print.go
:
func (p *pp) fmtPointer(value reflect.Value, verb rune) {
// ...
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice,
reflect.UnsafePointer:
u = value.Pointer()
// ...
}
但是你应该重构而不是比较函数值地址。
你无法比较功能。它需要存储在一个变量中,并将其作为指针引用。