Variables let us store and reuse information in a program. They help keep data organized and easy to change.
Why variables are needed in Go
Introduction
Syntax
Go
var variableName type = value
// or using shorthand inside functions
variableName := valueUse var to declare a variable with a type and optional initial value.
Inside functions, you can use := to declare and assign a variable quickly.
Examples
age and set it to 30.Go
var age int = 30
name and assign "Alice" using shorthand.Go
name := "Alice"isActive without setting a value (defaults to false).Go
var isActive bool
Sample Program
This program shows how to create variables to store a player's name and score. It prints them, then changes the score and prints it again.
Go
package main import "fmt" func main() { var score int = 10 playerName := "Bob" fmt.Println("Player:", playerName) fmt.Println("Score:", score) // Update score score = 20 fmt.Println("Updated Score:", score) }
Important Notes
Variables make your code flexible by letting you change values without rewriting the whole program.
Always give variables clear names so your code is easy to understand.
Summary
Variables store information to use later in your program.
They help keep data organized and easy to update.
Use var or shorthand := to create variables in Go.
Practice
1. Why do we need variables in Go programming?
easy
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 CQuick 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
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 BQuick 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
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 AQuick 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
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 DQuick 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
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 AQuick 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 unknown
10 questions ~5 mineasy hard
