GO Interfaces and Polymorphism 1 — Questions and Answers
Question 1: How does Go determine if a type implements an interface?
- The type must declare it with `implements`
- The type must embed the interface
- The type must have all methods in the interface with matching signatures (Correct answer)
- The type must be registered with the compiler
Correct answer: The type must have all methods in the interface with matching signatures
Go uses implicit interface satisfaction: a type implements an interface if it defines all the interface's methods with identical signatures, requiring no explicit declaration.
Question 2: What is the empty interface `interface{}` (or `any`) equivalent to?
- A nil pointer
- A type that holds any value since all types satisfy it (Correct answer)
- A map of type metadata
- An interface with one method
Correct answer: A type that holds any value since all types satisfy it
The empty interface has no methods, so every type satisfies it, allowing `interface{}` (or `any` in Go 1.18+) to hold a value of any type.
Question 3: What is a type assertion in Go?
- Declaring a variable as a specific type at compile time
- Extracting the concrete value and type from an interface at runtime (Correct answer)
- Casting between numeric types
- Checking interface satisfaction at compile time
Correct answer: Extracting the concrete value and type from an interface at runtime
A type assertion `v.(T)` extracts the underlying concrete value of type `T` from an interface value, panicking if the concrete type is not `T`.
Question 4: What does a type switch allow you to do in Go?
- Convert between numeric types
- Select a code branch based on the dynamic type of an interface value (Correct answer)
- Match strings with regex
- Switch between package versions
Correct answer: Select a code branch based on the dynamic type of an interface value
A type switch uses `switch v := i.(type)` to branch on the concrete type stored in an interface, running the matching case.
Question 5: Can a Go interface be embedded in another interface?
- No, only structs can be embedded
- Yes, to compose larger interfaces from smaller ones (Correct answer)
- Yes, but only one level deep
- No, Go does not support interface composition
Correct answer: Yes, to compose larger interfaces from smaller ones
Go supports interface embedding, allowing you to compose interfaces; for example, `io.ReadWriter` embeds `io.Reader` and `io.Writer`.
Question 6: What is the `Stringer` interface in Go?
- An interface requiring a `Print() string` method
- An interface from `fmt` requiring `String() string`, used for custom string formatting (Correct answer)
- A built-in type for string manipulation
- A method to compare strings
Correct answer: An interface from `fmt` requiring `String() string`, used for custom string formatting
The `fmt.Stringer` interface has a single `String() string` method; types implementing it control their own output when formatted with `%v` or `%s`.
How does Go determine if a type implements an interface?