We use var to create a named place in the computer's memory to store data. This helps us keep and change values while the program runs.
Variable declaration using var in Go
var variableName type var variableName type = value
You can declare a variable with or without an initial value.
The type tells Go what kind of data the variable will hold, like int for numbers or string for text.
age as an integer and name as a string, without initial values.var age int
var name stringage is 30 and name is "Alice".var age int = 30 var name string = "Alice"
isActive with the value true.var isActive bool = trueThis program declares two variables using var. city is a string with the value "Paris". year is an integer declared first without a value, then assigned 2024. The program prints both values.
package main import "fmt" func main() { var city string = "Paris" var year int year = 2024 fmt.Println("City:", city) fmt.Println("Year:", year) }
If you declare a variable without assigning a value, Go gives it a default value (like 0 for numbers, empty string for text).
You can assign a value later after declaring the variable.
var creates a variable with a name and type.
You can give the variable a value right away or later.
Variables store data you want to use or change in your program.
Practice
var keyword do in Go?Solution
Step 1: Understand the role of
Thevarvarkeyword is used to declare variables in Go, giving them a name and a type.Step 2: Differentiate from other keywords
Other keywords likefunc,import, andforserve different purposes, not variable declaration.Final Answer:
Declares a variable with a name and type -> Option DQuick Check:
vardeclares variables [OK]
- Confusing var with func or import
- Thinking var creates loops
- Assuming var is for constants
count using var in Go?Solution
Step 1: Recall Go variable declaration syntax
In Go, the correct syntax isvar variableName type, sovar count intis correct.Step 2: Check other options for syntax errors
Options A, C, and D do not follow Go's syntax rules for variable declaration.Final Answer:
var count int -> Option AQuick Check:
var name type = var count int [OK]
- Placing var after variable name
- Using '=' without value
- Swapping type and var keywords
package main
import "fmt"
func main() {
var x int = 10
var y int
y = 5
fmt.Println(x + y)
}Solution
Step 1: Identify variable values
Variablexis declared with value 10,yis declared then assigned 5.Step 2: Calculate the sum printed
The program printsx + ywhich is 10 + 5 = 15.Final Answer:
15 -> Option CQuick Check:
10 + 5 = 15 [OK]
- Assuming uninitialized y is zero without assignment
- Confusing variable declaration with initialization
- Ignoring the assignment to y
package main
func main() {
var name string = "Go"
var age
age = 10
}Solution
Step 1: Check variable declarations
The variableageis declared without a type or initial value, which is invalid in Go.Step 2: Understand Go's requirement for var declarations
Go requires a type or an initial value when usingvar. Here,agehas neither.Final Answer:
Missing type for variable 'age' in declaration -> Option AQuick Check:
var needs type or value [OK]
- Declaring var without type or value
- Assuming assignment after declaration fixes type
- Confusing var with := shorthand
a as an integer with value 5 and b as a string with value "hello", using var. Which is the correct way to do this in Go?Solution
Step 1: Understand type declaration for multiple variables
In Go, when declaring multiple variables with different types, each must be declared separately or with explicit types.Step 2: Evaluate each option
var a, b int = 5, "hello" tries to declare both as int but assigns a string to b, which is invalid. var a = 5, b = "hello" uses shorthand but mixes types without explicit declaration. var a int, b string = "hello", 5 mixes types incorrectly in one statement. var a int = 5; var b string = "hello" correctly declares each variable with its type and value.Final Answer:
var a int = 5; var b string = "hello" -> Option BQuick Check:
Separate var declarations for different types [OK]
- Trying to declare different types in one var line
- Mixing types without explicit declaration
- Using shorthand := incorrectly with var
Ready to prove it?
Pass the test and this pattern moves unknown
