Go Constants

Go Tutorial

 Go Constants (const) – Complete Guide with Examples

In Go (Golang), constants are fixed values that cannot change during program execution. They make your code safer, more readable, and less error-prone.

If you understand how It work in Go, you’ll write cleaner logic, avoid magic numbers, and prevent accidental changes.


 What Are Constants in Go?

A constant is a variable whose value cannot be changed once declared.

Go uses the const keyword to define It.

Syntax

Example

  •  Value is fixed
  •  Cannot be reassigned
  •  Known at compile time

 Why Constants Are Important in Go?

They are important because they:

  •  Prevent accidental value changes
  •  Improve code readability
  •  Remove magic numbers
  •  Make programs more predictable
  •  Help the compiler optimize code

 In Go, They are preferred over variables whenever values don’t change.


 Declaring Constants in Go

Single Constant

Multiple Constant

Multiple Constants in a Block

  •  Cleaner syntax
  •  Better organization

 Typed vs Untyped Constants

Go supports typed and untyped constants.

Untyped Constants (Default)

  •  Flexible
  •  Can be used as int, float, etc.

Typed Constants


 

  •  Strict type checking
  • Less flexible

 


 Constants Cannot Be Changed


 

  •  Prevents bugs
  •  Enforced at compile time

 Constants vs Variables in Go

Featureconstvar
Value change NoYes
Runtime assignment NoYes
Compile-time Yes No
Used for fixed values Yes No

Rule:
If the value never changes → use const.


 Constant Expressions

It can be expressions, not just values.

  •  Evaluated at compile time
  • No runtime cost

 The iota Keyword (Very Important)

iota is used to create auto-incrementing constants.

Example

Output:

A = 0
B = 1
C = 2

iota with Custom Values

Output:

Low = 1
Medium = 2
High = 3
  •  Useful for enums
  •  Clean and readable

 iota with Skipped Values

  • _ ignores a value
  •  Starts counting from Jan = 1

 Constant and Data Types

Allowed constant types in Go:

Valid

  •  Numbers
  •  Strings
  •  Booleans

Invalid

  •  Slices
  •  Maps
  •  Structs

 Best Practices for Go Constants

  •  Use It for fixed values
  •  Use meaningful constant names
  •  Prefer constant blocks
  •  Use iota for enums
  •  Avoid magic numbers

 Common Mistakes :

  •  Trying to change a constant
  •  Using constant for runtime values
  •  Overusing typed constant
  •  Not using iota where needed

 Interview Questions:

1. Can constant change in Go?
No, It cannot be changed.

2. Are Go constant evaluated at runtime?
No, they are evaluating at compile time.

3. What is iota used for?
For creating auto-incrementing constant.

4. Can constant be slices or maps?
No, only basic types are allowing.

5. Difference between const and var?
const is immutable, var is mutable.


 Summary

  •  It store fixed values
  •  Declared using const
  •  Evaluated at compile time
  • iota simplifies enums
  •  Improve safety and readability

You may also like...