Bird
Raised Fist0
Goprogramming~10 mins

Empty interface in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Empty interface
Declare empty interface variable
↓
Assign any value to variable
↓
Use variable as any type
↓
Type assertion or reflection to get original type
↓
End
An empty interface variable can hold any value. You assign values, then use type assertion to get the original type.
Execution Sample
Go
package main

import "fmt"

func main() {
    var x interface{}
    x = 42
    fmt.Println(x)
    x = "hello"
    fmt.Println(x)
}
This code shows an empty interface variable holding an int, then a string, printing both.
Execution Table
StepActionVariable xOutput
1Declare x as empty interfacenil
2Assign 42 to x42 (int)
3Print x42 (int)42
4Assign "hello" to x"hello" (string)
5Print x"hello" (string)hello
💡 Program ends after printing both values.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
xnil42 (int)"hello" (string)"hello" (string)
Key Moments - 2 Insights
Why can variable x hold both an int and a string?
Because x is declared as an empty interface, it can hold any type. See execution_table steps 2 and 4 where x changes type.
What happens if you try to use x as a specific type without assertion?
You must use type assertion to convert x back to its original type. Without it, Go won't know the actual type stored in x.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of x after step 2?
A"hello" (string)
B42 (int)
Cnil
D0
💡 Hint
Check the 'Variable x' column at step 2 in the execution_table.
At which step does x hold a string value?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Variable x' column in execution_table for string values.
If you want to get the int value from x after step 2, what must you do?
AUse type assertion to int
BDirectly use x as int
CConvert x to string
DAssign x to a new variable without assertion
💡 Hint
Refer to key_moments about type assertion necessity.
Concept Snapshot
Empty interface in Go is written as interface{}.
It can hold any type of value.
Assign any value to it without error.
To use the stored value as its original type, use type assertion.
Useful for generic containers or functions.
Full Transcript
This visual execution shows how an empty interface variable x in Go can hold any type of value. Initially, x is declared with no value (nil). Then, x is assigned the integer 42 and printed, showing 42. Next, x is assigned the string "hello" and printed, showing hello. The variable tracker shows x changing from nil to 42 (int) to "hello" (string). Key moments explain that the empty interface allows any type and that type assertion is needed to use the stored value as its original type. The quiz questions check understanding of the variable's value at different steps and the need for type assertion. This helps beginners see step-by-step how empty interfaces work in Go.

Practice

(1/5)
1. What does an empty interface interface{} represent in Go?
easy
A. A type that can hold any value of any type
B. A type that holds only integer values
C. A type that holds only string values
D. A type that cannot hold any value

Solution

  1. Step 1: Understand the empty interface definition

    The empty interface interface{} has no methods, so it can hold any type of value.
  2. Step 2: Recall Go's type system

    Since all types implement zero or more methods, and the empty interface requires none, all types satisfy it.
  3. Final Answer:

    A type that can hold any value of any type -> Option A
  4. Quick Check:

    Empty interface = any type [OK]
Hint: Empty interface means it accepts any type of value [OK]
Common Mistakes:
  • Thinking it holds only specific types like int or string
  • Confusing it with interfaces that have methods
  • Assuming it cannot hold values
2. Which of the following is the correct syntax to declare a variable of empty interface type in Go?
easy
A. var x interface{}
B. var x interface
C. var x emptyinterface{}
D. var x interface(void)

Solution

  1. Step 1: Recall the syntax for empty interface

    The empty interface is written as interface{} with empty braces.
  2. Step 2: Check each option

    Only var x interface{} is valid Go syntax for declaring an empty interface variable.
  3. Final Answer:

    var x interface{} -> Option A
  4. Quick Check:

    Empty interface syntax = interface{} [OK]
Hint: Empty interface always uses empty braces {} after interface [OK]
Common Mistakes:
  • Omitting the braces {}
  • Using incorrect keywords like emptyinterface
  • Adding invalid parameters inside interface
3. What will be the output of the following Go code?
var val interface{} = 42
fmt.Println(val)
medium
A. interface{}
B. 42
C. Compilation error
D. <nil>

Solution

  1. Step 1: Understand variable assignment

    The variable val is declared as empty interface and assigned the integer 42.
  2. Step 2: Print the value

    Printing val outputs the stored value, which is 42.
  3. Final Answer:

    42 -> Option B
  4. Quick Check:

    Empty interface holds 42, prints 42 [OK]
Hint: Empty interface prints stored value directly [OK]
Common Mistakes:
  • Expecting the type name instead of value
  • Thinking it causes a compile error
  • Assuming it prints <nil> when assigned a value
4. Identify the error in the following Go code snippet:
var x interface{}
x = 3.14
fmt.Println(x + 1)
medium
A. No error, prints 4.14
B. Prints <nil>
C. Compile error: invalid operation on interface{}
D. Runtime panic due to type assertion failure

Solution

  1. Step 1: Analyze the variable type

    Variable x is of type interface{}, holding a float64 value 3.14.
  2. Step 2: Check the operation

    Adding 1 to x is invalid because interface{} does not support arithmetic operations directly.
  3. Final Answer:

    Compile error: invalid operation on interface{} -> Option C
  4. Quick Check:

    Cannot do math directly on interface{} [OK]
Hint: Cannot do math on interface{} without type assertion [OK]
Common Mistakes:
  • Assuming interface{} behaves like the stored type automatically
  • Expecting runtime panic instead of compile error
  • Ignoring the need for type assertion
5. Given a slice of empty interfaces holding mixed types:
items := []interface{}{42, "hello", 3.14, true}

Which code snippet correctly counts how many elements are of type int?
hard
A. count := 0 for _, v := range items { if v.Type() == int { count++ } } fmt.Println(count)
B. count := 0 for _, v := range items { if v == int { count++ } } fmt.Println(count)
C. count := 0 for _, v := range items { if reflect.TypeOf(v) == "int" { count++ } } fmt.Println(count)
D. count := 0 for _, v := range items { if _, ok := v.(int); ok { count++ } } fmt.Println(count)

Solution

  1. Step 1: Understand type assertion syntax

    To check if an interface{} holds an int, use v.(int) with the comma-ok idiom.
  2. Step 2: Evaluate each option

    count := 0 for _, v := range items { if _, ok := v.(int); ok { count++ } } fmt.Println(count) uses correct type assertion and counts ints. The other options use invalid comparisons or incorrect reflection usage.
  3. Final Answer:

    count := 0 for _, v := range items { if _, ok := v.(int); ok { count++ } } fmt.Println(count) -> Option D
  4. Quick Check:

    Use type assertion with comma-ok to check type [OK]
Hint: Use type assertion with comma-ok to check interface{} type [OK]
Common Mistakes:
  • Comparing interface{} directly to a type
  • Using reflect.TypeOf(v) == "int" (string instead of type)
  • Calling Type() method which does not exist on interface{}
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