Wanted to understand more about Const


(Raj Singh) #1

Hi,

I would like to understand why below code gives the output as 7 7 7 :

  1. package main
  2. import “fmt”
  3. const (
  4. i = 7
  5. j
  6. k
  7. )
  8. func main() {
  9. fmt.Println(i, j, k)
  10. }

(Karl Benedict) #2

because if the type/value is not specified for the constant, it will be taken from the previous constant.


(Raj Singh) #3

I tried searching documentation for the same but could not find it. I did read about iota but for that we need to have iota while assigning value. Please can you share documentation link if possible.


(Karl Benedict) #4

https://golang.org/ref/spec#Constant_declarations