Go中你用对枚举了么?

开发 后端
枚举是强类型编程语言中的一种类型,由一组名称和值组成。通常用来在编程语言中充当常量的标识符。

[[377256]]

枚举的本质是什么,我们天天写代码用枚举,那啥是枚举啊。wiki上是这么说的

  • In computer programming, an enumerated type (also called enumeration, enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. An enumerated type can be seen as a degenerate tagged union of unit type. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but are not specified by the programmer as having any particular concrete representation in the computer’s memory; compilers and interpreters can represent them arbitrarily.

用人话说就是

枚举是强类型编程语言中的一种类型,由一组名称和值组成。通常用来在编程语言中充当常量的标识符。

没毛病,我们也确实是这样使用的。比如上学的时候,经常写c的小玩具代码,c标准里面提供了enum关键字,写起来比较直白,使用的时候和struct类似,需要enum week这样写,c里面默认枚举值是从0开始,int类型,其实c里面就是把枚举当做int类型来用的。

  1. #include<stdio.h>  
  2.    
  3. enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};  
  4.    
  5. int main()  
  6. {  
  7.     enum week day;             // 需要加 enum 关键字 
  8.     day = Wed;  
  9.     printf("%d",day);          // 输出 2 
  10.     int i;  
  11.     for (i=Mon; i<=Sun; i++){  // 可以直接把枚举类型赋值给int类型 
  12.       printf("%d ", i);        // 输出 0,1,2,3,4,5,6 
  13.     } 
  14.     return 0;  
  15. }   

上面的例子没问题,在初始化的时候,枚举值默认情况下,编译器会从分配0开始的值,例如上面的Mon=0,Tue=1…,但是也会想不按照编译器的默认分配方式,由我自己分配,那怎么写呢,看下面的例子:

  1. #include <stdio.h>  
  2. enum day {sunday = 1, monday, tuesday = 5,  
  3.           wednesday, thursday = 10, friday, saturday};  
  4.    
  5. int main()  
  6. {  
  7.     printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,  
  8.             wednesday, thursday, friday, saturday); // 输出1 2 5 6 10 11 12 
  9.     return 0;  
  10. }  

也就是说,枚举里面可以按任何顺序将值分配给某个名称。所有未分配的名称都会把值作为前一个名称的值加一。

其实,定义几个常量的事,是不是用宏这个东西更好呢,比如这么写

  1. #define sunday  0  
  2. #define monday  1  
  3. #define tuesday 2 

但是老师说了,尽量别用宏,不是说宏不好,宏也好,编译器替换,没有运行期啥事,多快啊,但是有几个问题:

1)宏没有作用域一说 2)枚举是类型安全的

扯的有点远了,现在回来看看Go里面的枚举怎么写。当然也很简单了,官方教导我们这么写:

  1. type ByteSize float64 
  2.  
  3. const ( 
  4.     _           = iota 
  5.     KB ByteSize = 1 << (10 * iota) 
  6.     MB 
  7.     GB 

Go里面更简洁了,直接把enum关键字去掉了,其实从Go的角度看,枚举不就是常量么,搞这么多语法糖干嘛,Go里面提供了一个关键字iota可以实现常量的递增,同时也支持手动赋值,iota和手动赋值结合起来,就可以实现类似c里面的效果

  1. const ( 
  2.     A0 = iota 
  3.     A1 = iota 
  4.     A2 = iota 
  5. fmt.Println(A0, A1, A2) // "0 1 2" 

可以 简写成这样

  1. const ( 
  2.     A0 = iota 
  3.     A1 
  4.     A2 

也可以从1开始

  1. const ( 
  2.     A1 = iota + 1 
  3.     A2 
  4.     A3 
  5. fmt.Println(A1, A2, A3) // "1 2 3" 

或者跳过某个值

  1. const ( 
  2.     C1 = iota + 1 
  3.     _ 
  4.     C3 
  5.     C4 
  6. fmt.Println(C1, C3, C4) // "1 3 4" 

看到这里你或许有个疑问,这里的枚举其实就是常量么,那怎么写是字符串类型的枚举呢,你可能会说,当然是用字符串常量了。但是那只是字符串常量了,没有枚举的性质。我可能想要的是一种字符串到值的枚举类型。思考再三,看我这种写法是否可以:

  • 步骤一:创建一个新的int类型
  • 步骤二:使用iota表示值
  • 步骤三:给这个新的类型一个String的方法
  1. type Direction int 
  2.  
  3. const ( 
  4.     North Direction = iota 
  5.     East 
  6.     South 
  7.     West 
  8.  
  9. func (d Direction) String() string { 
  10.     return [...]string{"North""East""South""West"}[d] 

使用的时候

  1. var d Direction = North 
  2. fmt.Print(d) 
  3. switch d { 
  4. case North: 
  5.     fmt.Println(" goes up."
  6. case South: 
  7.     fmt.Println(" goes down."
  8. default
  9.     fmt.Println(" stays put."

当然还有一种方法,比较stupid

  1. type weekday string 
  2.  
  3. func (w weekday) isWeekday() weekday { 
  4.     return w 
  5.  
  6. type Weekday interface { 
  7.     isWeekday() weekday 
  8.  
  9. const ( 
  10.     Monday   = weekday("Monday"
  11.     Tuesday  = weekday("Tuesday"
  12.     Wendsday = weekday("Wendsday"
  13.     Thursday = weekday("Thursday"
  14.     Friday   = weekday("Friday"
  15.     Saturday = weekday("Saturday"
  16.     Sunday   = weekday("Sunday"
  17. // 使用 
  18. func main() { 
  19.     var d1 = weekday.Monday 
  20.     var d2 = weekday.Tuesday 
  21.  
  22.     fmt.Println(d1, d2, d1 == d2, d1 == weekday.Monday) 

如果使用struct表示枚举,那其实还可以使用反射的方式,比如下面这样写:

  1. import ( 
  2.    "reflect" 
  3.  
  4. type weekday struct { 
  5.    Monday, Tuesday, Wendsday, Thursday, Friday, Saturday, Sunday int 
  6.  
  7. func (c weekday) Get(id string) int { 
  8.    vo := reflect.ValueOf(c) 
  9.    typeVo := vo.Type() 
  10.  
  11.    for i := 0; i < vo.NumField(); i++ { 
  12.       if typeVo.Field(i).Name == id { 
  13.          return vo.Field(i).Interface().(int
  14.       } 
  15.    } 
  16.    return 0 
  17.  
  18. var weekdayEnum = weekday { 
  19.    Monday:   1, 
  20.    Tuesday:  2, 
  21.    Wendsday: 3, 
  22.    Thursday: 4, 
  23.    Friday:   5, 
  24.    Saturday: 6, 
  25.    Sunday:   7 

本文转载自微信公众号「码小菜」,可以通过以下二维码关注。转载本文请联系码小菜公众号。

 

责任编辑:武晓燕 来源: 码小菜
相关推荐

2018-07-01 08:34:09

缓存数据服务

2019-12-10 10:13:58

HTTPNginxRedirect

2021-11-29 09:45:57

枚举Go代码

2022-07-25 14:54:32

前端

2021-03-16 22:44:18

Go语言开发

2022-06-09 10:42:47

GoJSON

2020-05-06 20:40:03

Go编程语言

2021-09-02 12:10:52

Go语言枚举类型

2022-08-15 08:06:49

Go语言类型

2022-02-21 18:16:38

Go语言枚举

2021-09-13 07:53:31

Go错误处理

2022-05-18 08:00:26

函数劫持JavaScript钩子函数

2019-08-27 08:24:17

简历技能工作

2009-07-20 10:18:49

PHP 5.3命名空间

2021-06-09 07:15:20

Go枚举技巧

2021-11-08 10:58:08

变量依赖图排序

2024-01-18 00:16:07

2020-06-23 14:09:49

枚举JDK场景

2021-09-13 07:23:52

Go Set 设计

2013-06-09 10:37:14

架构框架
点赞
收藏

51CTO技术栈公众号