「Go言語」iotaのリセットで定数を定義するサンプル
書式
const ( 定数1 = iota 略 ) const ( 定数3 =iota 略 )
iotaは連番の数字を取得します。iotaの値は0に戻りたい場合、再度constをリセットします。
使用例
package main
import "fmt"
const (
tA = iota
tB
tC
)
const (
tD = iota
tE
)
func main() {
fmt.Println("iotaは連場の数値を取得する")
fmt.Println(tA)
fmt.Println(tB)
fmt.Println(tC)
fmt.Println("iotaの値は0に戻る")
fmt.Println(tD)
fmt.Println(tE)
}
実行結果
C:\study\skill\golang>go run 121.go iotaは連場の数値を取得する 0 1 2 iotaの値は0に戻る 0 1