I have the following structs and they follow this structure:
A
is the interface, and B
, C
, D
are all types with interface A
.
I have a slice of variables args
all with type with interface A
, each of which can be B
, C
, D
type specifically.
I'd like to write a for loop to judge if all variables in the slice fall into the same dynamic type.
I wrote the following code:
var existingTyp A
for i, d := range args {
switch typ := d.(type) {
case *B, *C, *D:
if existingTyp == nil {
existingTyp = typ
} else {
if typ != existingTyp {
panic("error!")
}
}
}
How to revise the code to achieve what I want?
Aucun commentaire:
Enregistrer un commentaire