或者在条件内部

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

Or inside condition

问题

我有这段代码:

  1. if ev, ok := evt.(*ATypeEvent); ok {
  2. //处理 ATypeEvent
  3. } else if ev, ok := evt.(*BTypeEvent); ok {
  4. //处理 BTypeEvent
  5. } else if ev, ok := evt.(*CTypeEvent); ok {
  6. //处理 CTypeEvent
  7. }

现在我有另外三种事件类型,它们都可以归类到其他三种类型之一,我认为我需要一个或运算。

但是经过多次尝试,我还没有找到正确的方法。下面的代码是不起作用的:

  1. if ev, ok := evt.(*ATypeEvent) || evt.(*XTypeEvent); ok {
  2. //处理 ATypeEvent 和 X
  3. } else if ev, ok := evt.(*BTypeEvent) || evt.(*YTypeEvent); ok {
  4. //处理 BTypeEvent 和 Y
  5. } else if ev, ok := evt.(*CTypeEvent) || evt.(*ZTypeEvent); ok {
  6. //处理 CTypeEvent 和 Z
  7. }

也不是这样写:

  1. if ev, ok := evt.(*ATypeEvent) || ev, ok := evt.(*XTypeEvent); ok {

或者这样写:

  1. if ev, ok := (evt.(*ATypeEvent) || evt.(*XTypeEvent ) ); ok {

如何正确实现这个功能呢?

英文:

I have this code:

  1. if ev, ok := evt.(*ATypeEvent); ok {
  2. //process ATypeEvent
  3. } else if ev, ok := evt.(*BTypeEvent); ok {
  4. //process BTypeEvent
  5. } else if ev, ok := evt.(*CTypeEvent); ok {
  6. //process CTypeEvent
  7. }

It so now happens that I have 3 more event types, which all fit into one of the other 3 - I'd think I need a an OR.

But after several tries, I haven't been able to figure how to do it.
This doesn't work:

  1. if ev, ok := evt.(*ATypeEvent) || evt.(*XTypeEvent); ok {
  2. //process ATypeEvent and X
  3. } else if ev, ok := evt.(*BTypeEvent) || evt.(*YTypeEvent); ok {
  4. //process BTypeEvent and Y
  5. } else if ev, ok := evt.(*CTypeEvent) || evt.(*ZTypeEvent); ok {
  6. //process CTypeEvent and Z
  7. }

nor something like

  1. if ev, ok := evt.(*ATypeEvent) || ev, ok := evt.(*XTypeEvent); ok {

nor

  1. if ev, ok := (evt.(*ATypeEvent) || evt.(*XTypeEvent ) ); ok {

How can this be done correctly?

答案1

得分: 1

请稍等,我会为您进行翻译。

英文:

Use a type switch as explained in Effective Go, a highly recommended resource for you to read and understand many things in Go:

  1. switch v := ev.(type) {
  2. case *ATypeEvent, *XTypeEvent:
  3. // process ATypeEvent and X
  4. case *BTypeEvent, *YTypeEvent:
  5. // process BTypeEvent and Y
  6. case *CTypeEvent, *ZTypeEvent:
  7. // process CTypeEvent and Z
  8. default:
  9. // should never happen
  10. log.Fatalf("error: unexpected type %T", v)
  11. }

As for why your approach didn't work, Go's || and && operators require values of type bool and result in a single value of type bool, so assigning to ev, ok won't work as you wanted, nor will using a type assertion as a Boolean value. Without a type switch, you're stuck doing something like this:

  1. if ev, ok := evt.(*ATypeEvent); ok {
  2. //process ATypeEvent
  3. } else if ev, ok := evt.(*XTypeEvent); ok {
  4. //process XTypeEvent
  5. } else if ...

答案2

得分: 1

另一种选择是在接口上定义一个处理 evt 的方法。

  1. func (a *ATypeEvent) Process(...) ... {
  2. //处理 ATypeEvent
  3. }
  4. func (x *XTypeEvent) Process(...) ... {
  5. //处理 XTypeEvent
  6. }
  7. func (b *BTypeEvent) Process(...) ... {
  8. //处理 BTypeEvent
  9. }
  10. 以此类推
英文:

Another option is to define a method on the interface for evt.

  1. func (a *ATypeEvent) Process(...) ... {
  2. //process ATypeEvent
  3. }
  4. func (x *XTypeEvent) Process(...) ... {
  5. //process XTypeEvent
  6. }
  7. func (b *BTypeEvent) Process(...) ... {
  8. //process BTypeEvent
  9. }

and so on.

huangapple
  • 本文由 发表于 2017年4月29日 07:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/43690146.html
匿名

发表评论

匿名网友

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

确定