how to compare funcs in golang

huangapple go评论120阅读模式
英文:

how to compare funcs in golang

问题

我正在尝试为我的包编写一个测试,但在比较函数时失败了。以下是我实际在做的事情:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type HandlerFunc func(cmd interface{})
  7. type Bus struct {
  8. handlers map[reflect.Type]HandlerFunc
  9. }
  10. func (bus *Bus) RegisterHandler(cmd interface{}, handler HandlerFunc) {
  11. bus.handlers[reflect.TypeOf(cmd)] = handler
  12. }
  13. func (bus *Bus) GetHandler(cmd interface{}) HandlerFunc {
  14. t := reflect.TypeOf(cmd)
  15. for kind, handler := range bus.handlers {
  16. if t == kind {
  17. return handler
  18. }
  19. }
  20. return nil
  21. }
  22. func New() *Bus {
  23. return &Bus{
  24. handlers: make(map[reflect.Type]HandlerFunc),
  25. }
  26. }
  27. type RegisterUserCommand struct {}
  28. func main() {
  29. bus := New()
  30. handler := func (cmd interface{}) {}
  31. bus.RegisterHandler(&RegisterUserCommand{}, handler)
  32. retrieved := bus.GetHandler(&RegisterUserCommand{})
  33. if retrieved != handler {
  34. fmt.Println("Not the same!")
  35. return
  36. }
  37. fmt.Println("Same!")
  38. }

retrievedhandler进行比较会导致以下错误:

  1. invalid operation: (func(interface {}))(retrieved) != handler (func can only be compared to nil)

如何正确测试我检索到的函数与之前添加的函数是相同的?

英文:

I'm attempting write a test for my package and failing at comparing funcs. Here's essentially what i'm doing.

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type HandlerFunc func(cmd interface{})
  7. type Bus struct {
  8. handlers map[reflect.Type]HandlerFunc
  9. }
  10. func (bus *Bus) RegisterHandler(cmd interface{}, handler HandlerFunc) {
  11. bus.handlers[reflect.TypeOf(cmd)] = handler
  12. }
  13. func (bus *Bus) GetHandler(cmd interface{}) HandlerFunc {
  14. t := reflect.TypeOf(cmd)
  15. for kind, handler := range bus.handlers {
  16. if t == kind {
  17. return handler
  18. }
  19. }
  20. return nil
  21. }
  22. func New() *Bus {
  23. return &Bus{
  24. handlers: make(map[reflect.Type]HandlerFunc),
  25. }
  26. }
  27. type RegisterUserCommand struct {}
  28. func main() {
  29. bus := New()
  30. handler := func (cmd interface{}) {}
  31. bus.RegisterHandler(&RegisterUserCommand{}, handler)
  32. retrieved := bus.GetHandler(&RegisterUserCommand{})
  33. if retrieved != handler {
  34. fmt.Println("Not the same!")
  35. return
  36. }
  37. fmt.Println("Same!")
  38. }

Comparing retrieved with handler causes the following error

  1. invalid operation: (func(interface {}))(retrieved) != handler (func can only be compared to nil)

How can i properly test the function i'm retrieving is the same one added previously?

答案1

得分: 4

鉴于您无法比较函数,您可以以不同的方式编写测试。您可以让handler在测试中设置一个布尔值,并通过调用它并检查布尔值是否更改来检查是否获得了正确的函数。

以下是一个示例:

  1. func main() {
  2. bus := New()
  3. called := false
  4. handler := func (cmd interface{}) { called = true }
  5. bus.RegisterHandler(&RegisterUserCommand{}, handler)
  6. bus.GetHandler(&RegisterUserCommand{})(nil)
  7. if called {
  8. fmt.Println("我们得到了正确的处理程序!")
  9. return
  10. }
  11. fmt.Println("我们没有得到正确的处理程序")
  12. }
英文:

Given that you can't compare functions, you can write your test in a different way. You can make handler set a boolean value in your test and check that you've got the right function by calling it and seeing if the boolean changes.

Here's an example:

  1. func main() {
  2. bus := New()
  3. called := false
  4. handler := func (cmd interface{}) { called = true }
  5. bus.RegisterHandler(&RegisterUserCommand{}, handler)
  6. bus.GetHandler(&RegisterUserCommand{})(nil)
  7. if called {
  8. fmt.Println("We got the right handler!")
  9. return
  10. }
  11. fmt.Println("We didn't get the right handler")
  12. }

huangapple
  • 本文由 发表于 2015年4月29日 10:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/29933547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定