GO Interfaces and Polymorphism 2 — Questions and Answers
Question 1: Which of the following best describes polymorphism in Go?
- Method overloading by parameter count
- Using interface variables to call methods on different concrete types (Correct answer)
- Using generics only
- Inheritance from base structs
Correct answer: Using interface variables to call methods on different concrete types
Go achieves polymorphism through interfaces: a variable of interface type can hold any concrete type that satisfies it, and method calls dispatch to the concrete type.
Question 2: What happens when you call a method on a nil interface value?
- Returns zero value
- Calls the method on a zero-value struct
- Panics with 'nil pointer dereference' (Correct answer)
- Silently does nothing
Correct answer: Panics with 'nil pointer dereference'
Calling a method on a nil interface panics because there is no underlying concrete type or value to dispatch the call to.
Question 3: What is the difference between a nil interface and an interface holding a nil pointer?
- They are identical
- A nil interface has no type; an interface with a nil pointer has a type but nil value (Correct answer)
- A nil interface panics; an interface with nil pointer does not
- There is no difference in Go
Correct answer: A nil interface has no type; an interface with a nil pointer has a type but nil value
A nil interface (`var i I = nil`) has both type and value as nil; an interface holding a typed nil pointer has a type set but a nil value, so it is not equal to nil.
Question 4: Which built-in Go function can be used to verify at compile time that a type implements an interface?
- verify()
- assert()
- A blank identifier assignment: `var _ MyInterface = (*MyType)(nil)` (Correct answer)
- interface.Check()
Correct answer: A blank identifier assignment: `var _ MyInterface = (*MyType)(nil)`
The idiom `var _ MyInterface = (*MyType)(nil)` causes a compile error if `*MyType` does not implement `MyInterface`, serving as a compile-time check.
Question 5: Can a value type and a pointer type both satisfy the same interface in Go?
- No, only pointer types can satisfy interfaces
- Yes, if both define the required methods with matching receivers (Correct answer)
- Yes, but only if they share all methods
- No, receiver types must match exactly
Correct answer: Yes, if both define the required methods with matching receivers
Both `T` and `*T` can satisfy interfaces if they each define the required methods with the appropriate receiver type.
Question 6: What does it mean for an interface to be satisfied implicitly in Go?
- The compiler adds missing methods automatically
- A type satisfies an interface without any explicit declaration, just by having the required methods (Correct answer)
- The interface is optional and never checked
- Interfaces require a keyword to opt in
Correct answer: A type satisfies an interface without any explicit declaration, just by having the required methods
Go interfaces are satisfied implicitly: a type need only implement the required methods — no `implements` keyword or explicit registration is needed.
Which of the following best describes polymorphism in Go?