当前位置:网站首页>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!
边栏推荐
猜你喜欢

棕榈油罐区数字化转型

Leetcode71. 简化路径

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!.

GRUB2的零日漏洞补丁现已推出

QT commonly used global macro definitions

【Day_12 0507】查找组成一个偶数最接近的两个素数

TCP百万并发服务器优化调参

C语言理论--笔试面试基础稳固

今年最火爆的词:商业分析,看这一篇就够了!

统信软件、龙芯中科等四家企业共同发布《数字办公安全创新方案》
随机推荐
以消费场景为驱动的CMDB要怎么建?
How to use the Golang coroutine scheduler scheduler
【Day_12 0507】二进制插入
SQL的substring_index()用法——MySQL字符串截取
关于MySql中explain结果filtered的理解
2022年MySQL最新面试题
今年最火爆的词:商业分析,看这一篇就够了!
bat 批示处理详解-2
MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
GTK修改pixmap像素,提取pixmap像素RGB值
【Day_10 0428】密码强度等级
浅谈大数据背景下数据库安全保障体系
Review实战经典:2 种封装风格,你偏爱哪种?
【Day_12 0507】查找组成一个偶数最接近的两个素数
【Day_11 0506】 最近公共祖先
Leetcode73. Matrix Zeroing
B001 - 基于STM32的智能生态鱼缸
golang json returns null
实现mnist手写数字识别
QT基础功能,信号、槽