# How to use iota in Golang?

Go ProgrammingServer Side ProgrammingProgramming

#### Cyber Security : Go from Zero to Hero

Most Popular

60 Lectures 5.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

Iota in Go is used to represent constant increasing sequences. When repeated in a constant, its value gets incremented after each specification. In this article, we will explore the different ways in which we can use iota in Go.

Let's first consider a very basic example, where we will declare multiple constants and use iota.

## Example 1

Consider the code shown below

package main
import (
"fmt"
)
const (
first = iota
second = iota
third = iota
)
func main() {
fmt.Println(first, second, third)
}

## Output

If we run the command go run main.go, then we will get the following output in the terminal.

0 1 2

We can also omit the repetition of the iota keyword in the above example. Consider the code shown below.

## Example 2

package main
import (
"fmt"
)
const (
first = iota
second
third
)
func main() {
fmt.Println(first, second, third)
}

## Output

If we run the command go run main.go, then we will get the following output in the terminal.

0 1 2

It is not necessary to start the iota with the default value; we can start it with 1 also.

## Example 3

Consider the code shown below

package main
import (
"fmt"
)
const (
first = iota + 1
second
third
)
func main() {
fmt.Println(first, second, third)
}

## Output

If we run the command go run main.go, then we will get the following output in the terminal.

1 2 3

We can also skip the values while using iota.

## Example 4

Consider the code shown below.

package main
import (
"fmt"
)
const (
first = iota + 1
second
_
fourth
)
func main() {
fmt.Println(first, second, fourth)
}

## Output

If we run the command go run main.go, then we will get the following output in the terminal.

1 2 4
Updated on 22-Feb-2022 05:32:09