鉴于此代码:
type Philosopher int
const (
Epictetus Philosopher = iota
Seneca
)
func Quote(who Philosopher) string {
fmt.Println("t: ", reflect.TypeOf(who))
switch who {
case Epictetus:
return "First say to yourself what you would be;
and do what you have to do"
case Seneca:
return "If a man knows not to which port he sails,
No wind is favorable"
}
return "nothing"
}
调用Quote(5)将打印Foo.Philosopher为 5 的类型。为什么类型检查器没有抱怨,因为这是类型安全枚举应该做的事情,即限制值的范围?
翻过高山走不出你
BIG阳
RISEBY
相关分类