Original post

const (
        zero myConst = iota
        three = iota + 1
        five = iota + 1

func testIota() {
        // 3 4 5 why not 3 4 6
        fmt.Println(three, foure, five)
 why output is "3 4 5" not "3 4 6" executed in go version go1.13.10 linux/amd64

iota ist the “index” in the const. It monotically counts from 0 to positive infinity.

With an assignment in a const block that uses iota you don’t change any iota itself, only the formula how the consts value is calculated. So in case of five you basically say, that go shall calculate the value the same way as it already did, by adding 1 to iota, which at that place is 4.

got it, thanks a lot!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.