当前位置:网站首页>What is the implementation principle of Go iota keyword and enumeration type
What is the implementation principle of Go iota keyword and enumeration type
2022-08-01 18:00:00 【Yisuyun】
What is the implementation principle of Go iota keyword and enumeration type
This article mainly explains "what is the implementation principle of Go iota keyword and enumeration type". The content of the explanation in the article is simple and clear, easy to learn and understand. Please follow the ideas of the editor.Let's go deeper and study and learn "What is the implementation principle of Go iota keyword and enumeration type"!
1. iota keyword
iota is a constant counter in go language, which can only be used in constant expressions. Its value starts from 0. Each new line of iota in const increases by 1, and its value keeps increasing by 1 until it encounters the next one.const keyword, its value is reset to 0.
const ( a int = iota // 0 b b // 1 c c // 2 d d // 3)
In addition, when using iota, you can skip certain lines (it should be noted that spaces are not counted as lines).
const ( a = iota // 0 b b // 1 c c // 5 )
Also, iota can participate in mathematical operations.
Type Allergen Intconst (Igeggs Allergen = 1 << ota // 1 << 0 Which is 00000001 = 1 igChocolate // 1 << 1 WHICH is 0000000010 = 2 ignuts // 1 << 2 which is 00000100 = 4 IgStrawberries // 1 << 3 which is 00001000 = 8 IgShellfish // 1 << 4 which is 00010000 = 16)
And, on the same line, an iota can be used multiple times:
const (a, q int = IOTA, IoTa+1 // 0, 1 B, W // 1, 2 c, E/2, 3 d, r // 3, 4)
In summary, we can find that iota is similar to a line count in a const (), when we call it, no matter how many times we use it or not, itap will count each lineDo a count.
2. Enumeration type
Enumerations are used to represent types that contain only a limited number of fixed values, and are generally used in development to identify error codes or state machines.Take the state machine of an entity object, which usually corresponds to the field value of the object's corresponding record in the database that identifies the state.
Java provides us with ready-made implementations of enum classes.But not in Go, so we can use type to define a type and combine iota to achieve the effect of enumeration class:
type Season intconst ( Summer Season = 1 Autumn = 2 Winter = 3 Spring = 4)
Although the underlying implementation is still the int type, when we use the int type to compare directly with the Season type variable, there will be an error that the compilation fails, so that we can make type restrictions on the enumeration types we need.
Of course, we can also combine the above-mentioned itao to optimize:
type Season intconst (Summer Season = iota + 1 Autumn Winter Spring )
Thank you for reading. The above is the content of "What is the implementation principle of Go iota keyword and enumeration type?"I have a deeper understanding of what the implementation principle is, and the specific usage needs to be verified by everyone.Here is Yisuyun, the editor will push more articles about relevant knowledge points for you, welcome to pay attention!
边栏推荐
- 关于Mysql服务无法启动的问题
- 自定义注解实现日志打印时屏蔽特定字段不打印
- 广汽埃安“弹匣电池”,四大核心技术,出行安全保障
- 三维空间中点的插值
- 使用设备树时对应的驱动编程
- OpenCV installation, QT, VS configuration project settings
- 星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
- md5sum源码 可多平台编译
- 【Translation】OpenMetrics cultivated by CNCF becomes an incubation project
- Solve the problem that MySQL cannot insert Chinese data
猜你喜欢

How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.

基于ORB-SLAM2的改进代码

实现mnist手写数字识别

Shell nl命令详解(显示行号、读取文件)

关于单应性矩阵的若干思考

SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级

opencv语法Mat类型总结

基于BiGRU和GAN的数据生成方法

TCP million concurrent server optimization parameters

B005 - STC8 based single chip microcomputer intelligent street light control system
随机推荐
【Translation】OpenMetrics cultivated by CNCF becomes an incubation project
QT_事件类
SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级
MySQL关系型数据库事务的ACID特性与实现方法
史上最全的Redis基础+进阶项目实战总结笔记
SQL函数 TO_CHAR(三)
B005 – 基于STC8的单片机智能路灯控制系统
MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
B011 - 51-based multifunctional fingerprint smart lock
Leetcode75. 颜色分类
力扣每日一题-第45天-697. 数组的度
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
[供应链·案例篇]石油和天然气行业的数字化转型用例
B002 - Embedded Elderly Positioning Tracking Monitor
Go iota关键字与枚举类型实现原理是什么
【Day_12 0507】查找组成一个偶数最接近的两个素数
小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
TCP百万并发服务器优化调参
Review实战经典:2 种封装风格,你偏爱哪种?
EpiSci|片上系统的深度强化学习:神话与现实