📖 Scenario: You are building a simple program that can store different types of values in a single list. This is useful when you want to handle mixed data like numbers, text, and boolean values together.
🎯 Goal: Create a Go program that uses an empty interface to hold different types of values in a slice, then print each value.
📋 What You'll Learn
Create a slice called items that can hold any type of value using the empty interface.
Add exactly these values to items: 42, "hello", true.
Use a for loop with the variable item to go through items.
Print each item inside the loop.
💡 Why This Matters
🌍 Real World
Empty interfaces let Go programs handle data when the type is not known in advance, like reading JSON or working with mixed data.
💼 Career
Understanding empty interfaces is important for Go developers working with flexible data structures, APIs, or libraries that accept any type.
Progress0 / 4 steps
1
Create a slice of empty interface
Create a slice called items of type []interface{} and add these exact values: 42, "hello", true.
Go
Hint
Use []interface{} to create a slice that can hold any type.
2
Add a counter variable
Add a variable called count of type int and set it to 0 before the loop.
Go
Hint
Just write count := 0 before the loop.
3
Loop through the slice
Use a for loop with the variable item to go through items. Inside the loop, increase count by 1.
Go
Hint
Use for _, item := range items to loop and count++ to add one each time.
4
Print each item
Inside the for loop, use fmt.Println(item) to print each item. Also, import the fmt package at the top.
Go
Hint
Use fmt.Println(item) inside the loop to print each value.
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
Step 1: Understand the empty interface definition
The empty interface interface{} has no methods, so it can hold any type of value.
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.
Final Answer:
A type that can hold any value of any type -> Option A
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
Step 1: Recall the syntax for empty interface
The empty interface is written as interface{} with empty braces.
Step 2: Check each option
Only var x interface{} is valid Go syntax for declaring an empty interface variable.
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
Step 1: Understand variable assignment
The variable val is declared as empty interface and assigned the integer 42.
Step 2: Print the value
Printing val outputs the stored value, which is 42.
Final Answer:
42 -> Option B
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
Step 1: Analyze the variable type
Variable x is of type interface{}, holding a float64 value 3.14.
Step 2: Check the operation
Adding 1 to x is invalid because interface{} does not support arithmetic operations directly.
Final Answer:
Compile error: invalid operation on interface{} -> Option C
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
Step 1: Understand type assertion syntax
To check if an interface{} holds an int, use v.(int) with the comma-ok idiom.
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.
Final Answer:
count := 0
for _, v := range items {
if _, ok := v.(int); ok {
count++
}
}
fmt.Println(count) -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.