📖 Scenario: You are creating a simple program to calculate the area of a circle. You will use a constant for the value of Pi to keep your code clean and easy to update.
🎯 Goal: Build a Go program that defines a constant for Pi, uses it to calculate the area of a circle with a given radius, and prints the result.
📋 What You'll Learn
Create a constant named Pi with the value 3.14
Create a variable named radius with the value 5
Calculate the area of the circle using the formula Pi * radius * radius
Print the area with a descriptive message
💡 Why This Matters
🌍 Real World
Constants are used in programs to store values that do not change, like mathematical constants, configuration values, or fixed settings.
💼 Career
Understanding constants is important for writing clear, maintainable code in any software development job.
Progress0 / 4 steps
1
Create the radius variable
Create a variable called radius and set it to 5.
Go
Hint
Use radius := 5 inside the main function.
2
Define the Pi constant
Define a constant named Pi with the value 3.14 above the main function.
Go
Hint
Write const Pi = 3.14 before the main function.
3
Calculate the area
Create a variable called area and set it to the calculation Pi * radius * radius inside the main function.
Go
Hint
Use area := Pi * float64(radius) * float64(radius) to calculate the area.
4
Print the area
Print the message "Area of the circle:" followed by the value of area using fmt.Println.
Go
Hint
Use fmt.Println("Area of the circle:", area) to print the result.
Practice
(1/5)
1. What is the purpose of using const in Go programming?
easy
A. To declare a value that cannot be changed
B. To declare a variable that can change
C. To create a function
D. To start a loop
Solution
Step 1: Understand the role of const
The const keyword 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 that const declares a value that cannot be changed, while other options describe unrelated concepts.
Final Answer:
To declare a value that cannot be changed -> Option A
Quick Check:
Constants = fixed values [OK]
Hint: Constants hold fixed values, variables do not change [OK]
Common Mistakes:
Confusing constants with variables
Thinking const declares functions
Assuming const starts loops
2. Which of the following is the correct way to declare a constant named Pi with value 3.14 in Go?
easy
A. const Pi = 3.14
B. let Pi = 3.14
C. Pi := 3.14
D. var Pi = 3.14
Solution
Step 1: Recall Go syntax for constants
In Go, constants are declared using the const keyword followed by the name and value.
Step 2: Identify the correct syntax
const Pi = 3.14 uses const 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 A
Quick Check:
const keyword = correct declaration [OK]
Hint: Use const keyword for fixed values, not var or := [OK]