Home »
Golang »
Golang Reference
Golang iota Constant with Examples
Golang | iota Constant: Here, we are going to learn about the iota constant with examples.
Submitted by IncludeHelp, on October 12, 2021
iota Constant
In the Go programming language, iota is an untyped int type of value that is a predeclared identifier (represents the untyped integer ordinal number of the current const specification in a (usually parenthesized) const declaration). It is zero-indexed.
Syntax:
iota
Parameter(s):
Return Value:
None
Example 1:
// Golang program to demonstrate the
// example of iota constant
package main
import (
"fmt"
)
func main() {
const x = iota
// Printing the value and type
// of x
fmt.Printf("x: %T, %v\n", x, x)
// Declaring multiple constants
const (
a = iota
b = iota
c = iota
)
// Printing the values and types
// of a, b, and c
fmt.Printf("a: %T, %v\n", a, a)
fmt.Printf("b: %T, %v\n", b, b)
fmt.Printf("c: %T, %v\n", c, c)
}
Output:
x: int, 0
a: int, 0
b: int, 1
c: int, 2
Example 2:
// Golang program to demonstrate the
// example of iota constant
package main
import (
"fmt"
)
func main() {
// Declaring multiple constants
const (
a = iota
b
c
)
const (
x = iota + 10
y
z
)
// Printing the values and types
// of a, b, and c
fmt.Printf("a: %T, %v\n", a, a)
fmt.Printf("b: %T, %v\n", b, b)
fmt.Printf("c: %T, %v\n", c, c)
// Printing the values and types
// of x, y, and z
fmt.Printf("x: %T, %v\n", x, x)
fmt.Printf("y: %T, %v\n", y, y)
fmt.Printf("z: %T, %v\n", z, z)
}
Output:
a: int, 0
b: int, 1
c: int, 2
x: int, 10
y: int, 11
z: int, 12
Golang builtin Package »