golang 函数指针相等比较 | 您所在的位置:网站首页 › 怎么判断两个函数相同 › golang 函数指针相等比较 |
使用反射 reflect.ValueOf(functionName).Pointer() 反射详细参考:《golang reflect 反射 简介》https://blog.csdn.net/whatday/article/details/103191886 错误代码: package main import "fmt" func SomeFun() { } func main() { fmt.Println(SomeFun == SomeFun) }输出结果: ./func-pointers.go:12: invalid operation: SomeFun == SomeFun (func can only be compared to nil)正确代码: package main import "fmt" import "reflect" func SomeFun() {} func AnotherFun() {} func main() { sf1 := reflect.ValueOf(SomeFun) sf2 := reflect.ValueOf(SomeFun) fmt.Println(sf1.Pointer() == sf2.Pointer()) af1 := reflect.ValueOf(AnotherFun) fmt.Println(sf1.Pointer() == af1.Pointer()) }输出: true false
|
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |