I have two identical structures, which for disambiguation purposes have different types:
type BaseType struct {
id uint64
name string
}
type LabeledType1 BaseType
type LabeledType2 BaseType
There is one function in the whole chain which actually doesn't care about the LabeledType
, it just works with the BaseType
(because it does the exact same thing with both). The sender of the event has to send the labeled type, not the base type, because the actual type defines some post-behavior.
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
}
Now of course this doesn't compile:
cannot convert e (type *LabeledType1) to type BaseType
But I wonder, both types are assignable as far as I understand that concept, so there should be some easy conversion? I've tried type casting:
evt.(BaseType)
)
and also
BaseType(e)
I can't use a bool
inside BaseType
.
Use the (*BaseType)()
type conversion to convert a *LabeledType1
and *LabeledType2
to a *BaseType
:
func handle(evt interface{}) error {
switch e := evt.(type) {
case *LabeledType1:
return handleBaseEvent((*BaseType)(e))
}
...
}