dtslobe4694 2016-01-20 13:24
浏览 64
已采纳

如何在Go中比较2个功能?

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?

  • 写回答

3条回答 默认 最新

  • donglianer5064 2016-01-20 13:41
    关注

    Before going further: you should refactor and not compare function value addresses.

    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.

    Function values are not comparable. What you may do is compare if the addresses of the function values are the same (not the address of variables holding function values, but the function values themselves).

    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() to get the address of a function value.

    See this example (based on your code):

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

    Output (try it on the Go Playground):

    Expecting true: true
    Expecting false: false
    Expecting true: true
    

    Another option would be to use reflect.Value.Pointer() to get the address of the function values, this is exactly what the fmt package does: 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()
        // ...
    }
    

    But you should refactor and not compare function value addresses.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条