gpt4 book ai didi

function - 如何比较 Go 中的 2 个函数?

转载 作者:IT王子 更新时间:2023-10-29 00:34:28 25 4
gpt4 key购买 nike

例如,我有要比较的函数列表:

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

如果两个函数相同,比较的正确方法是什么?

最佳答案

在进一步讨论之前:您应该重构而不是比较函数值地址。

Spec: Comparison operators:

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 , 但如果你用 fmt 打印它包,它打印它的地址。所以你可以使用 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()
// ...
}

但是你应该重构而不是比较函数值地址。

关于function - 如何比较 Go 中的 2 个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34901307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com