当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
Go GORM事务实例分析
【Day_08 0426】两种排序方法
云原生全景图详解
Leetcode73. Matrix Zeroing
极化微波成像概述3
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
统信软件、龙芯中科等四家企业共同发布《数字办公安全创新方案》
Leetcode71. 简化路径
SQL的ROUND函数用法及其实例
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
OpenCV installation, QT, VS configuration project settings
小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
QT_QThread线程
How to use the Golang coroutine scheduler scheduler
SQL的substring_index()用法——MySQL字符串截取
QT_事件类
关于MySql中explain结果filtered的理解
2022年SQL经典面试题总结(带解析)
【Day_11 0506】 最近公共祖先









