0
0
Goprogramming~5 mins

Short variable declaration in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is short variable declaration in Go?
It is a way to declare and initialize a variable in one step using := instead of var and =.
Click to reveal answer
beginner
How do you declare a variable named count with value 10 using short variable declaration?
You write count := 10. This creates and sets count to 10.
Click to reveal answer
intermediate
Can you use short variable declaration outside functions in Go?
No, short variable declaration (:=) can only be used inside functions, not at package level.
Click to reveal answer
intermediate
What happens if you use := with a variable name that already exists in the same scope?
If at least one new variable is declared, it reuses existing variables and declares new ones. If none are new, it causes a compile error.
Click to reveal answer
beginner
Why is short variable declaration useful?
It makes code shorter and easier to read by combining declaration and initialization in one step.
Click to reveal answer
Which symbol is used for short variable declaration in Go?
A->
B=
C:=
D==
Where can you use short variable declaration in Go?
AAt package level only
BInside functions only
CAnywhere in the code
DOnly in main function
What happens if you write x := 5 when x already exists in the same scope?
AIt causes a compile error if no new variables are declared
BIt declares a new variable x and shadows the old one
CIt updates the value of x
DIt ignores the statement
Which of these is a correct short variable declaration?
Aa = 10
Bvar a := 10
Ca =: 10
Da := 10
Why might you prefer short variable declaration over var with = ?
AIt combines declaration and initialization in one step
BIt is longer but clearer
CIt works outside functions
DIt allows redeclaring variables globally
Explain how short variable declaration works in Go and when you can use it.
Think about where you write := and what it does.
You got /4 concepts.
    Describe what happens if you try to use := with only existing variables in the same scope.
    Consider Go's rules for redeclaring variables with :=
    You got /3 concepts.