0
0
Goprogramming~5 mins

Empty interface in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the empty interface in Go?
The empty interface in Go is written as interface{}. It can hold values of any type because every type implements at least zero methods.
Click to reveal answer
beginner
How do you declare a variable that can hold any type using the empty interface?
You declare it like this: var x interface{}. Then you can assign any value to x, like a string, int, or struct.
Click to reveal answer
intermediate
Why is the empty interface useful in Go?
It is useful because it allows functions or data structures to accept or store any type of value, making code flexible and generic.
Click to reveal answer
intermediate
How do you retrieve the original type from an empty interface variable?
You use type assertion, for example: value, ok := x.(int) checks if x holds an int and extracts it safely.
Click to reveal answer
intermediate
Can the empty interface be used to create a slice that holds different types?
Yes! For example, var list []interface{} can hold ints, strings, structs, etc., all in the same slice.
Click to reveal answer
What does interface{} represent in Go?
AAn interface that accepts only strings
BAn interface with one method
CAn interface that accepts any type
DAn interface that accepts only integers
How do you declare a variable that can hold any type in Go?
Avar x interface{}
Bvar x int
Cvar x string
Dvar x any
Which Go feature allows you to check the actual type stored in an empty interface variable?
AType declaration
BType embedding
CType conversion
DType assertion
Can a slice of type []interface{} hold different types of values?
ANo, only one type allowed
BYes, it can hold any types
COnly integers
DOnly strings
What happens if you try a wrong type assertion on an empty interface variable?
AIt causes a runtime panic if unchecked
BIt automatically converts the type
CIt returns zero value silently
DIt compiles but ignores the assertion
Explain what the empty interface is in Go and why it is useful.
Think about how Go treats types and interfaces.
You got /3 concepts.
    Describe how to safely extract a value from an empty interface variable.
    Remember the form: value, ok := x.(Type)
    You got /3 concepts.