我对如何检查对象的动态结构中是否存在属性感到困惑。即,如果我们有以下结构:
type Animal struct {
Name string
Origin string
}
type Bird struct {
Animal
Speed float32
CanFly bool
}
type Bear struct {
Animal
Lazy bool
}
现在我有一个函数用作参数:Animal
func checkAminalSpeed (a Animal){
// if the struct of current animal doesn't have the Speed attribute
// print ("I don't have a speed")
//otherwise, return the speed of this animal
}
此函数尝试检查变量的运行时类型以选择操作。
我想知道在这种情况下,如何编写此函数?谢谢!checkAminalSpeed
倚天杖
明月笑刀无情
相关分类