In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to use iota in the Go language. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Source enumeration
Recently, when making a requirement, there is a scenario that requires enumeration, with about 10 + enumerated types, rather than being written perfunctorily like defining a switch.
Const (
SwitchOff = 0
SwitchOn = 1
)
It is not exquisite enough.
So thought of iota, in-depth understanding, this little thing seems to have something.
Back to the requirements themselves-- enumerations. With iota, you don't have to display and define a lot of values.
Iota version is not used
Const (
ColorRed = 0
ColorOrange = 1
ColorYellow = 2
ColorGrassland = 3
ColorCyan = 4
ColorBlue = 5
ColorPurple = 6
)
Use the iota version
Const (
ColorRed = iota
ColorOrange
ColorYellow
ColorGrassland
ColorCyan
ColorBlue
ColorPurple
)
The effect of the two is the same, and so are the values corresponding to each enumeration. Iota starts at 0, and each line is incremented down. At first glance, iota looks high-end.
While exerting its power, iota has also been criticized by everyone.
For example, if you need to add a "gray" enumeration type at this time, no matter where the enumeration is inserted in the unused version of iota, define a specific value, such as 7.
In the use of iota version, if you add after ColorPurple, the corresponding value is 7, no problem.
But if it is in another position, it will upset the original balance, such as after the ColorGrassland.
Const (
ColorRed = iota / / 0
ColorOrange / / 1
ColorYellow / / 2
ColorGrassland / / 3
ColorGray / / 4
ColorCyan / / 5
ColorBlue / / 6
ColorPurple / / 7
)
As you can see, the values corresponding to the ColorCyan and subsequent enumerations have changed since the addition of ColorGray. If the values of each enumeration are already hard code in the code, such an adjustment would be catastrophic.
Iota is flexible, but it seems a little too flexible.
Seeing this, you think you already know iota. No, you don't. It's more flexible and complicated than you think.
The fancy play of iota
First of all, let's look at a delivery problem.
Const (
AA = iota
BB
_
DD
)
problem
What is the corresponding value of DD at this time?
With a little reasoning, it's obviously not 2, because there's an underscore in the middle.
Yes, the corresponding value of DD is 3. The underscore "_" here means to skip a certain value. The original value corresponding to this position should be 2, but it is not important to get it, so it is skipped with underscore. This usage is also consistent with the definition of Go corresponding to underscore.
For example, when traversing the map collection, you do not need to use the key value, you can write as
For _, value: = range testMap {
Fmt.Println (value)
}
Okay, let's move on to the next question.
Const (
AA = iota
BB
_
DD = iota + 1
EE
)
problem
What is the corresponding value of DD and EE at this time?
Unlike the previous example, DD = iota + 1 is reassigned after DD, that is, 1 is added to the original data, so the value of DD is 3-1-4.
If EE is not redefined later, the rule of DD will also be incremented by 1, that is, 5.
If you get the correct answer to the above question, the next one will not be difficult.
Const (
AA = iota
BB
_
DD = iota + 1
EE
FF = iota + 2
GG = iota
)
problem
What is the corresponding value of FF and GG at this time.
According to the previous question, DD and EE correspond to 4 and 5, respectively.
First of all, look at the FF here, notice that the FF here is not the value of the deferred EE plus 1, and then add 2, if it is deferred, FF = 6 + 2 = 8. But the value of FF is 7.
Whenever an enumeration is reset (that is, when you use iota to reassign values later), you need to go from the first enumerator to the current order, for example, from AA=0 to FF, where the order of FF is 5, and then add 2, that is, FF=5+2=7.
The value of GG uses the above method to get a value of 6.
Note: the above is my conclusion from the results. At first, it is difficult to understand the corresponding values of the enumerations here. After finding this rule, I found that the values run out of the program are the same as those verified by the rules.
Let's look at the last question.
Const (
AA, BB = iota + 1, iota + 2
CC, DD
)
problem
What are the corresponding values of AA, BB, CC and DD at this time
Only one rule needs to be understood here, and iota will only be added one per line.
So the iota in the first row here is 0, then the values of AA and BB are 0 and 2, respectively.
The following CC and DD are both deferred, and the corresponding iota increment is 1, and then the values are 1: 1 = 2 and 1: 2 = 3 according to the operation of iota+1 and iota+2, respectively.
Well, after completing the above, whether it is to send a sub-question or a proposition, I think you have a real understanding of the little thing iota.
Personal feeling, function to achieve thousands of items, understand and then use the first one.
Go back to the enumeration
Sometimes we use enumerations, not only to define its values, but also to have corresponding description information. We know that this is easy to implement in Java. After all, Java already has the concept of enumerations.
Let's take a look at two ways in which the Go implementation has an enumeration of description information.
Use map Mapping
Const (
ColorRed = iota
ColorOrange
ColorYellow
ColorGrassland
ColorCyan
ColorBlue
ColorPurple
)
Var ColorMap = map [int] string {
ColorRed: "Chi"
ColorOrange: orange
ColorYellow: "Huang"
ColorGrassland: "Green"
ColorCyan: "Qing"
ColorBlue: "Blue"
ColorPurple: purple
}
In this way, if you want to get the description information corresponding to ColorRed, you can write it as ColorMap [ColorRed].
Define enumerated types
Type Color int
Const (
ColorRed Color = iota
ColorOrange
ColorYellow
ColorGrassland
ColorCyan
ColorBlue
ColorPurple
)
Func (c Color) String () string {
Switch c {
Case ColorRed:
Return "Chi"
Case ColorOrange:
Return "orange"
Case ColorYellow:
Return "Yellow"
Case ColorGrassland:
Return "green"
Case ColorCyan:
Return "Qing"
Case ColorBlue:
Return "Blue"
Case ColorPurple:
Return "Purple"
}
If the color enumeration is defined as a Color type, then all enumerated values are of that type, and if you want to get the description information corresponding to ColorRed, you can write it as ColorRed.String ().
These are all the contents of the article "how to use iota in Go language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.