或者在条件内部

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

Or inside condition

问题

我有这段代码:

if ev, ok := evt.(*ATypeEvent); ok {
   //处理 ATypeEvent
} else if ev, ok := evt.(*BTypeEvent); ok {
   //处理 BTypeEvent
} else if ev, ok := evt.(*CTypeEvent); ok {
   //处理 CTypeEvent
}

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

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

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

也不是这样写:

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

或者这样写:

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

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

英文:

I have this code:

if ev, ok := evt.(*ATypeEvent); ok {
   //process ATypeEvent
} else if ev, ok := evt.(*BTypeEvent); ok {
   //process BTypeEvent
} else if ev, ok := evt.(*CTypeEvent); ok {
   //process CTypeEvent
}

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:

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

nor something like

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

nor

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:

switch v := ev.(type) {
case *ATypeEvent, *XTypeEvent:
    // process ATypeEvent and X
case *BTypeEvent, *YTypeEvent:
    // process BTypeEvent and Y
case *CTypeEvent, *ZTypeEvent:
    // process CTypeEvent and Z
default:
    // should never happen
    log.Fatalf("error: unexpected type %T", v)
}

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:

if ev, ok := evt.(*ATypeEvent); ok {
    //process ATypeEvent
} else if ev, ok := evt.(*XTypeEvent); ok {
    //process XTypeEvent
} else if ...

答案2

得分: 1

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

func (a *ATypeEvent) Process(...) ... {
  //处理 ATypeEvent
}

func (x *XTypeEvent) Process(...) ... {
  //处理 XTypeEvent
}

func (b *BTypeEvent) Process(...) ... {
  //处理 BTypeEvent
}

以此类推
英文:

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

func (a *ATypeEvent) Process(...) ... {
  //process ATypeEvent
}

func (x *XTypeEvent) Process(...) ... {
  //process XTypeEvent
}

func (b *BTypeEvent) Process(...) ... {
  //process BTypeEvent
}

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:

确定