What if changing one number could fix your whole program instantly?
Why Constants in Go? - Purpose & Use Cases
Imagine you are writing a program where you need to use the same value, like the number of days in a week, many times. You type the number 7 everywhere in your code manually.
Typing the same number repeatedly is slow and risky. If you want to change it later, you must find and update every single place. This can cause mistakes and bugs if you miss some spots.
Constants let you give a name to a fixed value once. Then you use that name everywhere. If you need to change the value, you update it in one place only, making your code safer and easier to manage.
daysInWeek := 7 fmt.Println(daysInWeek) // Later, if days change, you must update all 7s manually
const daysInWeek = 7 fmt.Println(daysInWeek) // Change daysInWeek once if needed, all uses update automatically
Constants make your code clear, reliable, and easy to update by naming fixed values just once.
Think of a recipe that always uses 2 cups of sugar. Instead of writing '2' everywhere, you name it 'sugarAmount'. If the recipe changes, you update 'sugarAmount' once, and the whole recipe stays correct.
Constants store fixed values with meaningful names.
They prevent errors from repeated manual typing.
Updating constants is simple and safe.
Practice
const in Go programming?Solution
Step 1: Understand the role of
Theconstconstkeyword is used to declare constants, which are fixed values that cannot be changed after being set.Step 2: Compare options with the definition
To declare a value that cannot be changed correctly states thatconstdeclares a value that cannot be changed, while other options describe unrelated concepts.Final Answer:
To declare a value that cannot be changed -> Option AQuick Check:
Constants = fixed values [OK]
- Confusing constants with variables
- Thinking const declares functions
- Assuming const starts loops
Pi with value 3.14 in Go?Solution
Step 1: Recall Go syntax for constants
In Go, constants are declared using theconstkeyword followed by the name and value.Step 2: Identify the correct syntax
const Pi = 3.14 usesconst Pi = 3.14, which is the correct syntax. Options A and C declare variables, and B uses invalid Go syntax.Final Answer:
const Pi = 3.14 -> Option AQuick Check:
const keyword = correct declaration [OK]
- Using var or := instead of const
- Using let which is not Go syntax
- Missing the const keyword
package main
import "fmt"
const greeting = "Hello"
func main() {
fmt.Println(greeting)
}Solution
Step 1: Understand constant usage in the program
The constantgreetingis declared with value "Hello" and printed in main.Step 2: Predict the output of fmt.Println
fmt.Println(greeting)prints the string value of the constant, which is "Hello" without quotes.Final Answer:
Hello -> Option DQuick Check:
Printing constant value = Hello [OK]
- Expecting the name 'greeting' to print
- Thinking it prints with quotes
- Assuming a compile error
package main
import "fmt"
const x = 10
func main() {
x = 20
fmt.Println(x)
}Solution
Step 1: Check constant reassignment
The code declaresxas a constant with value 10, then tries to assign 20 to it, which is not allowed.Step 2: Verify other possible errors
There are no other compilation errors; the only issue is reassignment of the constant.Final Answer:
Cannot assign to constant 'x' after declaration -> Option BQuick Check:
Constants cannot be reassigned [OK]
- Trying to assign new value to a constant
- Ignoring missing imports
- Confusing constants with variables
package main
import "fmt"
const (
A = iota
B
C = 5
D
)
func main() {
fmt.Println(A, B, C, D)
}Solution
Step 1: Understand iota behavior in constant blocks
iotastarts at 0 and increments by 1 for each line unless overridden.Step 2: Analyze each constant value
A = iota (0), iota increments to 1; B = iota (1), iota to 2; C = 5 (explicit), iota to 3; D = iota (3).Step 3: Confirm output
Printing A, B, C, D outputs: 0 1 5 3Final Answer:
0 1 5 3 -> Option CQuick Check:
iota increments except when overridden [OK]
- Assuming D equals C's value (5)
- Thinking iota resets after explicit value
- Expecting compilation error
Ready to prove it?
Pass the test and this pattern moves unknown
