Iota in Const block first time not starting with 0 value

Hi, I have below code, but as per my understanding “aaa” in first const block should be 0 when we first assign iota, where in this case its 3, and bbb is 4 since its starting at 3 because we already have 3 constants in first const block(0,1,2) for a, pi,&str1; is this expected and right-way? I thought it should start with 0 whenever we declared first time in const block. or I’m missing anything! Thank you

package main

import “fmt”
const(
a=35
pi=3.79879889
str1=“Jangalapalli”
aaa=iota
bbb
)
const (
aaa1=iota
bbb1
ccc1
ddd1
)
func main() {
fmt.Printf(“Value: %v\t type:%T\n”,a,a)
fmt.Printf(“Value: %v\t type:%T\n”,pi,pi)
fmt.Printf(“Value: %v\t type:%T\n”,str1,str1)

fmt.Printf("Value: %v\t type:%T\n",aaa,aaa)
fmt.Printf("Value: %v\t type:%T\n",bbb,bbb)

fmt.Printf("Value: %v\t type:%T\n",aaa1,aaa1)
fmt.Printf("Value: %v\t type:%T\n",bbb1,bbb1)
fmt.Printf("Value: %v\t type:%T\n",ccc1,ccc1)
fmt.Printf("Value: %v\t type:%T\n",ddd1,ddd1)

}

Thanks,
Subbareddy Jangalapalli

1 Like

Here are the docs: https://golang.org/ref/spec#Iota

Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. Its value is the index of the respective ConstSpec in that constant declaration, starting at zero. It can be used to construct a set of related constants:

That basically means iota is 0 for the first definition in a const block, 1 for the second, 2 for the third etc. It doesn’t matter wheter iota is used in the const block or not.

2 Likes