使用反射 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