🧱 Go Constants – Declare Fixed Values in Go Programs (2025 Guide)
🧲 Introduction – What Are Constants in Go?
In Go, constants represent fixed values that cannot be changed once defined. They are evaluated at compile time and are primarily used for values that remain unchanged throughout the execution of a program—like configuration values, mathematical constants, or enumerated types.
🎯 In this section, you’ll learn:
- How to declare constants using the
const
keyword - Differences between constants and variables
- Typed vs untyped constants
- How to group constants and use
iota
for enumerations
🔧 Declaring Constants in Go
✅ Basic Constant Declaration
Use the const
keyword:
const pi = 3.14
const language string = "Go"
- The type can be explicitly mentioned or inferred.
- Constants must be assigned at declaration — no delayed assignment like variables.
🆚 Constants vs Variables
Feature | const (Constant) | var / := (Variable) |
---|---|---|
Value Changes? | ❌ Cannot change after declaration | ✅ Can be reassigned |
Evaluated At | Compile time | Runtime |
Type Inference | ✅ Supported | ✅ Supported |
Use Cases | Configs, fixed strings, math | Dynamic logic, user inputs, counters |
📋 Typed vs Untyped Constants
Untyped (default behavior)
const x = 10
Can be used as int
, float64
, etc., based on context.
Typed
const y float64 = 10.0
Bound to float64
, strict in usage.
Untyped constants give more flexibility, especially when using them across different types.
🗂️ Grouping Constants
You can declare multiple constants together:
const (
AppName = "MyApp"
Version = "1.0"
Author = "Alice"
)
This improves readability and organization.
🔁 Enumerations Using iota
iota
is a special identifier in Go that simplifies the creation of incrementing constants, often used for enums.
const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
)
iota
resets to 0 with every const
block and increments automatically.
More advanced usage:
const (
A = 1 << iota // 1 (binary: 0001)
B // 2 (0010)
C // 4 (0100)
D // 8 (1000)
)
🧪 Real-World Example
package main
import "fmt"
const Pi = 3.1415
const (
StatusPending = iota
StatusApproved
StatusRejected
)
func main() {
fmt.Println("Pi:", Pi)
fmt.Println("Approved status code:", StatusApproved)
}
📤 Output:
Pi: 3.1415
Approved status code: 1
⚠️ Limitations of Constants
- Cannot use
const
for values determined at runtime (e.g.,time.Now()
). - Cannot assign to
const
once declared. const
can only be scalar values (numeric, string, boolean, etc.) — no slices, maps, or structs.
📌 Summary – Recap & Next Steps
Constants in Go provide a safe and reliable way to store fixed values that do not change during execution. They’re evaluated at compile time, improving code safety and efficiency.
🔍 Key Takeaways:
- Use
const
for values that never change - Typed constants are strict; untyped constants are flexible
- Use
iota
for generating enums and incrementing values - Constants improve code readability and maintainability
⚙️ Next: Dive into Go Data Types and understand how Go handles primitive and composite types.
❓ FAQs – Go Constants
❓ Can I reassign a value to a constant?
✅ No. Constants are immutable. Once declared, their value cannot be changed.
❓ Can constants be declared globally in Go?
✅ Yes. Constants can be declared at the package level and used throughout your code.
❓ What is iota
used for?
✅ iota
is used to create sequential or bitwise enumerated constants automatically.
❓ Can constants hold complex types like slices or maps?
✅ No. Constants can only hold basic types like string, int, float, bool.
❓ What’s the difference between typed and untyped constants?
✅ Untyped constants adapt based on context. Typed constants are bound to a specific type and used accordingly.
Share Now :