What is the first itoa
iota in the const is a constant counter golang language, can only be used in the expression ,, that is, within const constants in.
(Before the first row inside const) const keyword appears iota when it is reset to 0, const each new row iota constant declaration will be counted once.
Can be understood with reference to the line number, that is the row index is understood iota const statement block.
Examples on its properties as follows:
Its various characteristics described by way of example.
1, each time const appears, will make iota initialized to zero.
const a = iota // a=0
const (
b = iota //b=0
c //c=1
)
2, custom type
Since constant growth often contain a custom enumeration type, allowing you to rely on the compiler to complete the increment settings.
type Newtype int
const (
T1 Newtype = iota // 0
T2 // 1
T3 // 2
T4 // 3
)
3, the skip value
type AudioOutput int
const (
OutMute AudioOutput = iota // 0
OutMono // 1
OutStereo // 2
_
_
OutSurround // 5
)
4, bit mask expression
type Allergen int
const (
IgEggs Allergen = 1 << iota // 1 << 0 which is 00000001
IgChocolate // 1 << 1 which is 00000010
IgNuts // 1 << 2 which is 00000100
IgStrawberries // 1 << 3 which is 00001000
IgShellfish // 1 << 4 which is 00010000
)
5, the definition of magnitude
type ByteSize float64
const (
_ = iota // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * iota) // 1 << (10*1)
MB // 1 << (10*2)
GB // 1 << (10*3)
TB // 1 << (10*4)
PB // 1 << (10*5)
EB // 1 << (10*6)
ZB // 1 << (10*7)
YB // 1 << (10*8)
)
6, in the case of a line defined
No different with the ordinary form
const (
Apple, Banana = iota + 1, iota + 2
Cherimoya, Durian
Elderberry, Fig
)
iota next line growth, not made reference to it immediately.
// Apple: 1
// Banana: 2
// Cherimoya: 2
// Durian: 3
// Elderberry: 3
// Fig: 4
7, cut in the middle
Intermediate jump the queue, iota will no longer continue to be overwritten increment. But with another iota answer it, will continue to increment. Examples are as follows, 5 and the intermediate insert 6, below 5 itoa into contact 6 is not.
const(
a = iota
b = 5
c = iota
d = 6
e
f
)
Then print out the result is 052 666
Advantages and disadvantages
Use iota can simplify the definition, when you define enumeration useful. When the code needs to be changed, but also relatively easy to expand or modify. Also look at is some force inside the cell.
But itoa so the code is not so easily understood relative. It will increase the burden of understanding of the code. If usage coincides with a colleague itoa do not understand, but also do not want to learn will increase the possibility of digging.
So I recommend the appropriate use, that is, only with its simple features like. I claim: One of the basic requirements of the code is posters. Machine up and running no problem, people can be easily read is a good code.
Finally, remember that there is a similar strconv-itoa
Most languages will have a itoa function, which is very different with the const in the itoa. itoa function is used to convert a string of numeric string. golang in the itoa function is strconv.Itoa (int) string.
Clearly distinguish between the two, but also help to understand and remember.
Welcome to criticism and correction
Reference:
https://blog.csdn.net/xuduorui/article/details/78653895