我有两个相同的结构,为了消除歧义,它们有不同的类型:
type BaseType struct {
id uint64
name string
}
type LabeledType1 BaseType
type LabeledType2 BaseType
在整个链中有一个函数实际上并不关心
LabeledType
,它只适用于
BaseType
(因为两者的作用完全相同)。事件的发送者必须发送带标签的类型,而不是基类型,因为实际类型定义了一些post行为。
func handle(evt interface{}) error {
switch e := evt.(type) {
case *LabeledType1:
return handleBaseEvent(e)
case *LabeledType2:
return handleBaseEvent(e)
//there are other types
case *OtherType:
return handleOtherType(e)
}
}
func handleBaseEvent(evt *BaseType) {
//do stuff
}
cannot convert e (type *LabeledType1) to type BaseType
但我想知道,这两种类型都是
据我所知,这个概念,所以应该有一些简单的转换?我试过打字:
evt.(BaseType)
还有
BaseType(e)
bool
基本类型
.