# Golang 枚举详解

### 什么是枚举

const (
A = "a"
B = "b"
）


### 枚举语法示例

const (
Monday int = iota      // Monday  = 0
Tuesday               // Tuesday = 1
Wednesday             // Wednesday = 2
Thursday              // Thursday = 3
Friday                // Friday = 4
Saturday              // Saturday =5
Sunday                // Sunday = 6
)


### 枚举的使用疑问

#### 枚举与常量的区别

const (
Monday     int = 0            // Monday  = 0
Tuesday    int = 1            // Tuesday = 1
Wednesday  int = 2            // Wednesday = 2
Thursday   int = 3            // Thursday = 3
Friday     int = 4            // Friday = 4
Saturday   int = 5            // Saturday =5
Sunday     int = 6            // Sunday = 6
)


#### 枚举值只能为整形?

//枚举类型必须是整形吗，不能是浮点数或是别的类型?
enum E
{
monday=-2,
tuesday=1.5//有么有非int类型的枚举?
};


### 枚举高级用法

#### 从 1 开始枚举

const (
Monday = iota + 1     // Monday  = 1
Tuesday               // Tuesday = 2
Wednesday             // Wednesday = 3
Thursday              // Thursday = 4
Friday                // Friday = 5
Saturday              // Saturday = 6
Sunday                // Sunday = 7
)


#### 自定义枚举值

iota 默认从 0 开始且依次递增 1，同时也可以使用数学运算自定义枚举值

const (
Monday = iota + 1     // Monday = 0 + 1 = 1
Tuesday = iota + 2    // Tuesday = 1 + 2 = 3
Wednesday = iota * 2  // Wednesday = 2 * 2 = 2
Thursday              // Thursday = 3
Friday                // Friday = 4
Saturday              // Saturday =5
Sunday                // Sunday = 6
)


#### 跳过值的枚举

const (
Monday = iota         // Monday = 0
_                     // 1 被跳过
Tuesday               // Tuesday = 2
Wednesday             // Wednesday = 3
Thursday              // Thursday = 4
_                     // 5 被跳过
Friday                // Friday = 6
Saturday              // Saturday = 7
Sunday                // Sunday = 8
)


### 枚举使用场景

#### 限制参数类型

const (
Normal       = 200
Forbid       = 403
NetworkError = 502
)

// 该函数形参是整形
func HandlerStatus(statusCode int) {
fmt.Println(statusCode)
}

// 调用 HandlerStatus 随便什么整数都可以
func main() {
HandlerStatus(Normal)
HandlerStatus(404)
}

200
404


HandlerStatus 这个函数应该只能传 NormalForbidNetworkError 这三个常量，可以发现常量集之外的值也可以被正常调用，会使得程序不严谨。

// int 别名
type Code int

const (
Normal Code = iota
Forbid
NetworkError
)

func HandlerStatus(statusCode Code) {
fmt.Println(statusCode)
}

func main() {
HandlerStatus(Normal)
var notFount int = 1
// 这里会导致程序编译失败，因为 notFount 不是 Code 类型
HandlerStatus(notFount)
}


#### 使用 string 作为枚举值

// 声明一个 week 类型
type week int

const (
Monday week = iota    // Monday  = 0
Tuesday               // Tuesday = 1
Wednesday             // Wednesday = 2
Thursday              // Thursday = 3
Friday                // Friday = 4
Saturday              // Saturday =5
Sunday                // Sunday = 6
)

func main() {
var w week = Monday
switch w {
case Monday:
fmt.Println(Monday)
case Tuesday:
fmt.Println(Tuesday)
}
}

0


// 声明一个 week 类型
type week int

const (
Monday week = iota    // Monday  = 0
Tuesday               // Tuesday = 1
Wednesday             // Wednesday = 2
Thursday              // Thursday = 3
Friday                // Friday = 4
Saturday              // Saturday =5
Sunday                // Sunday = 6
)

// 实现 week 类型的 String() 方法
func (w week) String() string {
return [...]string{"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"}[w]
}

func main() {
var w week = Monday
switch w {
case Monday:
fmt.Println(Monday)
case Tuesday:
fmt.Println(Tuesday)
}
}



• 0
点赞
• 3
收藏
觉得还不错? 一键收藏
• 0
评论
07-25 2229
07-24 657
03-21 489
10-13 1491
04-04 571
06-09 5966
06-04 4278
01-19 5809
09-18
09-21
11-25 8097
02-06 933
12-04 464

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。