<3> go 枚举

来源:互联网 发布:传奇霸业转生升级数据 编辑:程序博客网 时间:2024/06/13 22:10

在go语言中,没有直接支持枚举的关键字,也就造成go没有直接枚举的功能。但是go提供另一种方法来实现枚举,那就是const+iota

// 实现枚举例子type State int// iota 初始化后会自动递增const (    Running State = iota // value --> 0    Stopped              // value --> 1    Rebooting            // value --> 2    Terminated           // value --> 3)func (this State) String() string {    switch this {    case Running:        return "Running"    case Stopped:        return "Stopped"    default:        return "Unknow"    }}func main() {    state := Stopped    fmt.Println("state", state)}// 输出 state Running// 没有重载String函数的情况下则输出 state 0
0 0
原创粉丝点击