您现在的位置是:亿华云 > 应用开发
Go中你用对枚举了么?
亿华云2025-10-08 20:53:40【应用开发】5人已围观
简介枚举的本质是什么,我们天天写代码用枚举,那啥是枚举啊。wiki上是这么说的In computer programming, an enumerated type (also called enumer
枚举的中用本质是什么,我们天天写代码用枚举,对枚那啥是中用枚举啊。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类型来用的。
#include<stdio.h> enum week{ Mon, Tue, Wed, Thur, Fri, Sat, Sun}; int main() { enum week day; // 需要加 enum 关键字 day = Wed; printf("%d",day); // 输出 2 int i; for (i=Mon; i<=Sun; i++){ // 可以直接把枚举类型赋值给int类型 printf("%d ", i); // 输出 0,b2b信息网1,2,3,4,5,6 } return 0; }上面的例子没问题,在初始化的时候,枚举值默认情况下,编译器会从分配0开始的值,例如上面的Mon=0,Tue=1…,但是也会想不按照编译器的默认分配方式,由我自己分配,那怎么写呢,看下面的例子:
#include <stdio.h> enum day { sunday = 1, monday, tuesday = 5, wednesday, thursday = 10, friday, saturday}; int main() { printf("%d %d %d %d %d %d %d", sunday, monday, tuesday, wednesday, thursday, friday, saturday); // 输出1 2 5 6 10 11 12 return 0; }也就是说,枚举里面可以按任何顺序将值分配给某个名称。所有未分配的名称都会把值作为前一个名称的值加一。
其实,定义几个常量的事,是不是用宏这个东西更好呢,亿华云比如这么写
#define sunday 0 #define monday 1 #define tuesday 2但是老师说了,尽量别用宏,不是说宏不好,宏也好,编译器替换,没有运行期啥事,多快啊,但是有几个问题:
1)宏没有作用域一说 2)枚举是类型安全的
扯的有点远了,现在回来看看Go里面的枚举怎么写。当然也很简单了,官方教导我们这么写:
type ByteSize float64 const ( _ = iota KB ByteSize = 1 << (10 * iota) MB GB )Go里面更简洁了,直接把enum关键字去掉了,其实从Go的角度看,枚举不就是常量么,搞这么多语法糖干嘛,Go里面提供了一个关键字iota可以实现常量的递增,同时也支持手动赋值,iota和手动赋值结合起来,就可以实现类似c里面的效果
const ( A0 = iota A1 = iota A2 = iota ) fmt.Println(A0, A1, A2) // "0 1 2"可以 简写成这样
const ( A0 = iota A1 A2 )也可以从1开始
const ( A1 = iota + 1 A2 A3 ) fmt.Println(A1, A2, A3) // "1 2 3"或者跳过某个值
const ( C1 = iota + 1 _ C3 C4 ) fmt.Println(C1, C3, C4) // "1 3 4"看到这里你或许有个疑问,这里的枚举其实就是常量么,香港云服务器那怎么写是字符串类型的枚举呢,你可能会说,当然是用字符串常量了。但是那只是字符串常量了,没有枚举的性质。我可能想要的是一种字符串到值的枚举类型。思考再三,看我这种写法是否可以:
步骤一:创建一个新的int类型 步骤二:使用iota表示值 步骤三:给这个新的类型一个String的方法 type Direction int const ( North Direction = iota East South West ) func (d Direction) String() string { return [...]string{ "North", "East", "South", "West"}[d] }使用的时候
var d Direction = North fmt.Print(d) switch d { case North: fmt.Println(" goes up.") case South: fmt.Println(" goes down.") default: fmt.Println(" stays put.") }当然还有一种方法,比较stupid
type weekday string func (w weekday) isWeekday() weekday { return w } type Weekday interface { isWeekday() weekday } const ( Monday = weekday("Monday") Tuesday = weekday("Tuesday") Wendsday = weekday("Wendsday") Thursday = weekday("Thursday") Friday = weekday("Friday") Saturday = weekday("Saturday") Sunday = weekday("Sunday") ) // 使用 func main() { var d1 = weekday.Monday var d2 = weekday.Tuesday fmt.Println(d1, d2, d1 == d2, d1 == weekday.Monday) }如果使用struct表示枚举,那其实还可以使用反射的方式,比如下面这样写:
import ( "reflect" ) type weekday struct { Monday, Tuesday, Wendsday, Thursday, Friday, Saturday, Sunday int } func (c weekday) Get(id string) int { vo := reflect.ValueOf(c) typeVo := vo.Type() for i := 0; i < vo.NumField(); i++ { if typeVo.Field(i).Name == id { return vo.Field(i).Interface().(int) } } return 0 } var weekdayEnum = weekday { Monday: 1, Tuesday: 2, Wendsday: 3, Thursday: 4, Friday: 5, Saturday: 6, Sunday: 7 }本文转载自微信公众号「码小菜」,可以通过以下二维码关注。转载本文请联系码小菜公众号。
很赞哦!(726)
相关文章
- 公司名字不但要与其经营理念、活动识别相统一,还要能反映公司理念,服务宗旨、商品形象,从而才能使人看到或听到公司的名称就能产生愉快的联想,对商店产生好感。这样有助于公司树立良好的形象。
- 一下域名,看有没有显示出你所解析的IP,如果有,就说明解析是生效的;如果没有,就说明解析是不生效的。
- 换新域名(重新来过)
- ICANN 规章禁止转移已经被记录或者在60天前内转移的域名。
- 3、查看排名
- 什么是im域名?新手需要了解im域名哪些?
- 比较短的域名方便用户记忆和传播,它带来的好处往往会超过其他类型的域名,如果你非要域名短而且还要包含关键词,那么往往会事与愿违,现在这种域名基本上是可遇而不可求的。
- ④注册门槛低
- 四、长串数字域名
- 为什么说注册域名注意细节?哪些我们不能忽视?
热门文章
站长推荐
2、定期提交和投标域名注册。例如,益华网络点击“立即预订”后,平台会抢先为客户注册域名。当然,一个域名可能会被多个客户预订,所以出价最高的人中标。
4、企业无形资产:通用网站已成为企业网络知识产权的重要组成部分,属于企业的无形资产,也有助于提升企业的品牌形象和技术领先形象。它是企业品牌资产不可或缺的一部分。
Status、Creation Date、Expiration Date
4、参加域名拍卖会
3、商标域名一经注册,就可以作为域名裁决过程中的主要信息之一。这可以大大增加公司被抢注的相关域名胜诉的机会。
4、选择一个安全的域名注册商进行域名注册
CNAME:对应解析的记录值为域名地址
互联网中的地址是数字的IP地址,域名解析的作用主要就是为了便于记忆。