100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Constants and iota in Go

Understand how const declarations work in Go and how iota generates auto-incrementing enumerated constants.

BasicsBeginner8 min readJul 8, 2026
Analogies

Introduction

Constants in Go are declared with the const keyword and must have values that are known at compile time, such as numeric literals, strings, booleans, or expressions built from other constants. Unlike variables, constants cannot be reassigned after declaration. Go also provides a special identifier, iota, that is used inside const blocks to generate a sequence of related, auto-incrementing values, commonly used to build enumerated-style constants.

🏏

Cricket analogy: The number of players allowed on a cricket field, 11, is a fixed rule known before any match is played, just like a Go constant must be known at compile time and can never be reassigned once declared.

Syntax

go
const Pi = 3.14159
const Greeting string = "Hello"

const (
	StatusActive = iota // 0
	StatusPaused        // 1
	StatusStopped        // 2
)

Explanation

A const declaration binds a name to a fixed value; the compiler must be able to evaluate that value without running the program, so you cannot assign the result of a function call to a constant. Inside a const( ) block, iota starts at 0 on the first line and increments by 1 for each subsequent constant specification, even if a line reuses the previous expression implicitly. iota resets to 0 at the start of every new const block, which makes it ideal for defining related groups of named integer constants (similar to enums in other languages) without manually numbering each one.

🏏

Cricket analogy: iota inside a const block is like a scorer numbering each over automatically starting from over 0, incrementing by one for every new over even if the bowler changes ends, and resetting to 0 whenever a fresh innings (new const block) begins.

Example

go
package main

import "fmt"

type Weekday int

const (
	Sunday Weekday = iota
	Monday
	Tuesday
	Wednesday
)

func main() {
	fmt.Println(Sunday, Monday, Tuesday, Wednesday)

	const KB = 1 << (10 * (iota + 1))
	fmt.Println("1 KB in bytes:", KB)
}

Output

go
0 1 2 3
1 KB in bytes: 1024

Key Takeaways

  • Constants are declared with const and their values must be known at compile time.
  • Constants cannot be reassigned after declaration, unlike variables.
  • iota starts at 0 in each const block and increments by 1 per line.
  • iota resets to 0 whenever a new const( ) block begins.
  • iota is commonly combined with bit shifts or expressions to build enum-like groups.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#ConstantsAndIotaInGo#Constants#Iota#Syntax#Explanation#StudyNotes#SkillVeris