This is a creation in Article, where the information may have evolved or changed.
Go language iota A bit strange, look at the following code:
Package MainImport ( "FMT")Const ( testmin =1TestAtestb = Iota testc) func Main(){FMT. Printf("testmin:%d\n", testmin) FMT. Printf("testa:%d\n", TestA) FMT. Printf("testb:%d\n", testb) FMT. Printf("testc:%d\n", testc)}
What is the execution result of the above code?
As a result, when you see the results, the usage of iota is basically mastered:
/*TestMin:-1TestA:-1TestB:2TestC:3*/
Let's look at one more example:
Package MainImport ( "FMT")Const (i=1<<iota j=3<<iota k l)func Main(){FMT. Println("i=", i) FMT. Println("j=", J) FMT. Println("k=", K) FMT. Println("l=", L)}
Execution Result:
i= 1j= 6k= 12l= 24
K equals 12,l equals 24 How is it got out?
i=1<<iota j=3<<iota k l
That is
i=1<<0 j=3<<1 k=3<<2 l=3<<3
Iota each occurrence, automatically add 1, and the preceding operand if not specified, the default is to use the previous one, here is 3;