当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
后台管理系统的权限思路
hcip第九天
数字化采购管理系统开发:精细化采购业务流程管理,赋能企业实现“阳光采购”
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
【Day_09 0427】走方格的方案数
史上最全的Redis基础+进阶项目实战总结笔记
C language theory--a solid foundation for the written test and interview
Detailed explanation of the working principle of crystal oscillator
QT常用全局宏定义
打开微信客服
B001 - Intelligent ecological fish tank based on STM32
Leetcode75. Color Classification
极化微波成像概述3
关于Mysql服务无法启动的问题
浅谈大数据背景下数据库安全保障体系
【Day_12 0507】查找组成一个偶数最接近的两个素数
Solve the problem that MySQL cannot insert Chinese data
opencv syntax Mat type summary
广汽埃安“弹匣电池”,四大核心技术,出行安全保障
tooltip control