Bird
Raised Fist0
Goprogramming~10 mins

Short variable declaration in Go - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare and initialize variable x with value 10 using short variable declaration.

Go
func main() {
    [1] := 10
    fmt.Println(x)
}
Drag options to blanks, or click blank then click option'
Avar x
Bx
Cint x
Dx var
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using var keyword with := operator causes syntax error.
Trying to declare type explicitly with short declaration is invalid.
2fill in blank
medium

Complete the code to declare two variables a and b with values 5 and 7 using short variable declaration.

Go
func main() {
    [1] := 5, 7
    fmt.Println(a, b)
}
Drag options to blanks, or click blank then click option'
A(a, b)
Bvar a, b
Ca, b
D[a, b]
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses or brackets around variable names is invalid.
Using var keyword with := causes syntax error.
3fill in blank
hard

Fix the error in the short variable declaration of count with value 100.

Go
func main() {
    [1] 100
    fmt.Println(count)
}
Drag options to blanks, or click blank then click option'
Avar count =
Bvar count :=
Ccount :
Dcount :=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using var keyword with := causes syntax error.
Using = instead of := for short declaration.
4fill in blank
hard

Fill both blanks to declare variables name and age with values "Alice" and 30 using short variable declaration.

Go
func main() {
    [1] := "Alice"
    [2] := 30
    fmt.Println(name, age)
}
Drag options to blanks, or click blank then click option'
Aname
Bage
Cvar
Dint
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to declare both variables in one statement with commas.
Using var keyword with := operator.
5fill in blank
hard

Fill all three blanks to declare variables width, height, and area where width and height are 5 and 10, and area is their product using short variable declaration.

Go
func main() {
    [1] := 5
    [2] := 10
    [3] := [1] * [2]
    fmt.Println(area)
}
Drag options to blanks, or click blank then click option'
Awidth
Bheight
Carea
Dsize
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using wrong variable names in declaration.
Trying to declare multiple variables in one line incorrectly.

Practice

(1/5)
1. What does the short variable declaration := do in Go?
easy
A. It assigns a value to an already declared variable outside a function.
B. It declares and initializes a new variable inside a function.
C. It declares a variable without initializing it.
D. It declares a global variable accessible everywhere.

Solution

  1. Step 1: Understand short variable declaration usage

    The := syntax declares and initializes a variable in one step inside functions.
  2. Step 2: Differentiate from other declarations

    It cannot be used without initialization or outside functions for global variables.
  3. Final Answer:

    It declares and initializes a new variable inside a function. -> Option B
  4. Quick Check:

    Short declaration = declare + initialize [OK]
Hint: Remember := means declare and assign inside functions [OK]
Common Mistakes:
  • Thinking := works outside functions
  • Using := without initializing
  • Confusing := with simple assignment =
2. Which of the following is the correct way to declare and initialize a variable using short variable declaration in Go?
easy
A. x =: 10
B. var x := 10
C. x := 10
D. x :== 10

Solution

  1. Step 1: Identify correct short declaration syntax

    The correct syntax is variable := value without the var keyword.
  2. Step 2: Check each option

    x := 10 matches the correct syntax. The other options have invalid syntax.
  3. Final Answer:

    x := 10 -> Option C
  4. Quick Check:

    Short declaration syntax = variable := value [OK]
Hint: Short declaration never uses 'var' keyword [OK]
Common Mistakes:
  • Adding 'var' with :=
  • Swapping := to =:
  • Using double equals or colons
3. What will be the output of this Go code snippet?
func main() {
    a := 5
    a := 10
    fmt.Println(a)
}
medium
A. Compilation error
B. 10
C. 5
D. 0

Solution

  1. Step 1: Analyze variable redeclaration

    Using a := 5 then a := 10 in the same scope causes redeclaration error.
  2. Step 2: Understand Go's redeclaration rules

    Short declaration requires at least one new variable; here a is redeclared without new variables, causing error.
  3. Final Answer:

    Compilation error -> Option A
  4. Quick Check:

    Redeclaring same var with := in same scope = error [OK]
Hint: Short declaration needs at least one new variable [OK]
Common Mistakes:
  • Assuming redeclaration overwrites variable
  • Expecting last value to print
  • Ignoring scope rules
4. Identify the error in this Go code snippet:
func main() {
    x := 10
    x := 20
    fmt.Println(x)
}
medium
A. x is redeclared in the same scope causing a compile error.
B. x is not initialized before use.
C. fmt.Println is used incorrectly.
D. No error, output will be 20.

Solution

  1. Step 1: Check variable redeclaration

    The variable x is declared twice with := in the same function scope, which is not allowed.
  2. Step 2: Understand Go's redeclaration rules

    Redeclaring a variable with := requires at least one new variable; here none is new, so compile error occurs.
  3. Final Answer:

    x is redeclared in the same scope causing a compile error. -> Option A
  4. Quick Check:

    Redeclaration without new vars = compile error [OK]
Hint: Redeclare only if at least one new variable appears [OK]
Common Mistakes:
  • Thinking redeclaration updates variable silently
  • Ignoring compile errors on redeclaration
  • Assuming output prints last assigned value
5. Given the code below, which line correctly uses short variable declaration to add a new variable c while updating a?
func main() {
    a := 1
    b := 2
    // Insert line here
    fmt.Println(a, b, c)
}
hard
A. a, b := 3, 4
B. a := 3; c := 4
C. c := 4
D. a, c := 3, 4

Solution

  1. Step 1: Understand short declaration with existing variables

    To update a and declare new c in one line, use a, c := 3, 4.
  2. Step 2: Check other options

    a, b := 3, 4 redeclares two existing variables without a new one, causing a compile error. a := 3; c := 4 redeclares existing a without a new variable (compile error). c := 4 declares c without updating a.
  3. Final Answer:

    a, c := 3, 4 -> Option D
  4. Quick Check:

    Short declaration updates existing + new vars = correct [OK]
Hint: Use := with at least one new variable to update and declare [OK]
Common Mistakes:
  • Trying to redeclare only existing vars with :=
  • Declaring new vars separately without updating existing
  • Forgetting to include new variable in declaration
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard