0
0
Goprogramming~5 mins

Variable declaration using var in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the var keyword do in Go?
It declares a variable with a specified type and optionally assigns it a value.
Click to reveal answer
beginner
How do you declare an integer variable named age using var?
You write:
var age int
This creates a variable age of type int with a default value 0.
Click to reveal answer
intermediate
Can you declare multiple variables at once using var? How?
Yes. Use parentheses and list variables:
var (
  x int
  y string
)
This declares x as int and y as string.
Click to reveal answer
beginner
What is the default value of a variable declared with var but not assigned?
It depends on the type: <br>- Numbers: 0 <br>- Strings: empty string <br>- Booleans: false <br>- Pointers: nil
Click to reveal answer
beginner
How to declare and assign a variable in one line using var?
Write:
var name string = "Alice"
This declares name as string and assigns "Alice".
Click to reveal answer
What is the correct way to declare a variable count of type int using var?
Avar count int
Bcount := int
Cint var count
Dvar int count
What is the default value of a string variable declared with var but not assigned?
Anil
Bempty string
C0
Dfalse
Which of these declares multiple variables correctly?
Avar x, y int
Bvar (x int, y string)
Cvar x int; y string
Dvar ( x int y string )
How do you declare and assign a variable name with value "Bob" using var?
Aname := "Bob"
Bvar name = "Bob"
Cvar name string = "Bob"
Dvar string name = "Bob"
What happens if you declare a variable with var but do not assign a value?
AIt keeps old memory value
BIt causes an error
CIt is nil always
DIt has a zero value depending on type
Explain how to declare a variable using var in Go and what happens if you don't assign a value.
Think about how Go handles variables when you only declare them.
You got /4 concepts.
    Describe how to declare multiple variables at once using var and why it might be useful.
    Consider grouping declarations for neatness.
    You got /4 concepts.