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
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
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
0 1 2 3
1 KB in bytes: 1024Key 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
1. What value does iota hold on the first line of a const block?
2. Which of the following is NOT allowed as a constant's value?
3. What happens to iota when a new const block starts?
4. Can you reassign a value to a constant after it is declared?
Was this page helpful?
You May Also Like
Variables and Data Types in Go
Learn how Go declares variables with var and :=, and the built-in types like int, float64, bool, and string.
Operators in Go
Explore arithmetic, comparison, logical, and bitwise operators in Go, including the unique AND NOT operator.
Type Conversion in Go
Learn why Go requires explicit type conversion between numeric and other types, and how to perform it safely.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics