📖 Scenario: Imagine you are baking cookies and need to keep track of how many cookies you have baked so far. You want to remember this number so you can tell your friend later.
🎯 Goal: You will create a simple Go program that uses a variable to store the number of cookies baked and then prints that number. This will show why variables are useful to remember information in a program.
📋 What You'll Learn
Create a variable to store the number of cookies baked
Assign a number to that variable
Print the value of the variable
💡 Why This Matters
🌍 Real World
Variables are like labeled boxes where you keep things you want to remember, such as scores in a game or counts of items.
💼 Career
Understanding variables is essential for all programming jobs because they are the basic way to store and manage data in software.
Progress0 / 4 steps
1
Create a variable to store the number of cookies baked
Write a line of Go code to create a variable called cookiesBaked of type int and set it to 12.
Go
Hint
Use the var keyword to create a variable, then give it a name, type, and value.
2
Add a helper variable for the message
Create a variable called message of type string and set it to "Number of cookies baked:".
Go
Hint
Use var message string = "..." to create a text variable.
3
Use variables to prepare the output
Write a line of code that uses fmt.Printf to print the message followed by the cookiesBaked number on the same line. Use the format string "%s %d\n".
Go
Hint
Use fmt.Printf with the format string and variables to print the message and number together.
4
Print the final message
Run the program and print the output showing the message and the number of cookies baked.
Go
Hint
Run the program to see the message and number printed together.
Practice
(1/5)
1. Why do we need variables in Go programming?
easy
A. To write comments in the code
B. To make the program run faster
C. To store and reuse data during program execution
D. To create functions
Solution
Step 1: Understand the purpose of variables
Variables hold data that can be used and changed while the program runs.
Step 2: Identify the correct use of variables
Variables help keep data organized and allow reuse without rewriting values.
Final Answer:
To store and reuse data during program execution -> Option C
Quick Check:
Variables store data = A [OK]
Hint: Variables hold data to use later in code [OK]
Common Mistakes:
Thinking variables speed up the program
Confusing variables with comments
Believing variables create functions
2. Which of the following is the correct way to declare a variable in Go?
easy
A. x := int 10
B. var x int = 10
C. int x = 10
D. declare x = 10
Solution
Step 1: Recall Go variable declaration syntax
In Go, variables can be declared using 'var' keyword followed by name, type, and value.
Step 2: Check each option's syntax
var x int = 10 uses 'var x int = 10' which is correct. Others do not follow Go syntax.
Final Answer:
var x int = 10 -> Option B
Quick Check:
Go variable declaration = var x int = 10 [OK]
Hint: Use 'var' keyword or ':=' to declare variables [OK]
Common Mistakes:
Using C-style declarations like 'int x = 10'
Mixing ':=' with type keyword incorrectly
Using non-Go keywords like 'declare'
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
var count int = 5
count = count + 3
fmt.Println(count)
}
medium
A. 8
B. Compilation error
C. 0
D. 5
Solution
Step 1: Understand variable initialization and update
Variable 'count' starts at 5, then 3 is added, so count becomes 8.
Step 2: Predict the printed output
The program prints the updated value of count, which is 8.
Final Answer:
8 -> Option A
Quick Check:
5 + 3 = 8 [OK]
Hint: Add values stored in variables to find output [OK]
Common Mistakes:
Assuming variable value stays 5
Thinking output is zero by default
Expecting a syntax error
4. Find the error in this Go code snippet:
package main
import "fmt"
func main() {
x := 10
var x int = 20
fmt.Println(x)
}
medium
A. Missing semicolon after declaration
B. No error, code runs fine
C. Incorrect import statement
D. Variable redeclaration error
Solution
Step 1: Analyze variable declarations
The code declares 'x' twice in the same scope: once with ':=' and once with 'var'.
Step 2: Understand Go's redeclaration rules
Go does not allow redeclaring a variable with the same name in the same scope, causing an error.
Final Answer:
Variable redeclaration error -> Option D
Quick Check:
Redeclaring variable in same scope = error [OK]
Hint: Don't declare same variable twice in one scope [OK]
Common Mistakes:
Thinking missing semicolon causes error
Believing import statement is wrong
Assuming code runs without error
5. You want to store a user's age and then increase it by 1 in Go. Which code correctly uses variables to do this?
hard
A. var age int = 30
age = age + 1
fmt.Println(age)
B. age := 30
age := age + 1
fmt.Println(age)
C. var age = "30"
age = age + 1
fmt.Println(age)
D. var age int
age = "31"
fmt.Println(age)
Solution
Step 1: Check variable declaration and type
var age int = 30
age = age + 1
fmt.Println(age) declares 'age' as int and initializes it with 30, which is correct.
Step 2: Verify increment operation and printing
var age int = 30
age = age + 1
fmt.Println(age) correctly adds 1 to 'age' and prints the result. Other options have redeclaration or type errors.
Final Answer:
var age int = 30
age = age + 1
fmt.Println(age) -> Option A
Quick Check:
Declare int, add 1, print = var age int = 30
age = age + 1
fmt.Println(age) [OK]
Hint: Declare once, update value with '=' operator [OK]
Common Mistakes:
Redeclaring variable with ':=' twice
Mixing string and int types
Assigning string to int variable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.