ランボルギーニ・イオタ、いいよねー、かっこいいよねー、ってそうじゃなくてw
GO言語での iota のお話。
iotaとは、
Within a constant declaration, the predeclared identifier iota
represents successive untyped integer constants. It is reset to 0 whenever the reserved word const
appears in the source and increments after each ConstSpec. It can be used to construct a set of related constants:
コンスタント値の定義内で、Integer型としてiota型を使う事ができます。これは「Const」命令を区切りとして、ソース内に書かれた変数が0から順番の数値で初期化セットされるコンスタント値です。(by片桐)
片桐超訳によると、こういうもんらしい。早速実験
package main
import fmt "fmt"
const (
one = iota;
pr = iota;
)
func main() {
fmt.Printf("今、pr の中身は、%d\n", pr)
}
結果は
ってことで、1。これは、丸かっこ()に挟まれている部分を1ブロックとして考えて、oneが最初でiotaだから0、prが二番目でiotaだから1、ってことなのね。つぎに、ちょっとPrの位置をずらしてみると
package main
import fmt "fmt"
const (
one = iota;
two = iota;
pr = iota;
)
func main() {
fmt.Printf("今、pr の中身は、%d\n", pr)
}
三番目になると、2になる。面白いのは、iotaにすると、ブロックの最初からの相対位置で決まるらしくて、
package main
import fmt "fmt"
const (
one = 1
two = 2
three = 100;
four = 20;
fice = 80
pr = iota;
)
func main() {
fmt.Printf("今、pr の中身は、%d\n", pr)
}
これを実行したら、
6番目の位置にあるから、0相対で5、ってことになる。
おもしろい、これw 使い方はどんなのがあるんだろう。