An interface in Go is a type that defines a set of method signatures. Any type that implements these methods satisfies the interface automatically.
Click to reveal answer
beginner
How do you define an interface in Go?
You define an interface using the type keyword followed by the interface name and the interface keyword with method signatures inside curly braces.
Example:
type Speaker interface {
Speak() string
}
Click to reveal answer
beginner
True or False: In Go, a type must explicitly declare it implements an interface.
False. In Go, a type implements an interface implicitly by having the required methods. No explicit declaration is needed.
Click to reveal answer
intermediate
What happens if a type does not implement all methods of an interface it is assigned to?
The Go compiler will raise an error because the type does not satisfy the interface. All methods must be implemented to satisfy the interface.
Click to reveal answer
intermediate
Explain how interfaces help in Go programming.
Interfaces allow writing flexible and reusable code by defining behavior without specifying implementation. Different types can implement the same interface, enabling polymorphism.
Click to reveal answer
How do you declare an interface named 'Mover' with a method 'Move()' in Go?
Afunc Mover() interface { Move() }
Binterface Mover { Move() }
Ctype Mover struct { Move() }
Dtype Mover interface { Move() }
✗ Incorrect
In Go, interfaces are declared with 'type' keyword followed by the interface name and 'interface' keyword with method signatures inside braces.
If a type has all methods required by an interface, what must you do to implement that interface?
ANothing, it implements automatically
BExplicitly declare it implements the interface
CEmbed the interface in the type
DUse a special keyword 'implements'
✗ Incorrect
Go uses implicit implementation. If a type has all methods, it automatically satisfies the interface.
What error occurs if a type does not implement all interface methods?
ANo error, it works fine
BRuntime panic
CCompile-time error
DLinker error
✗ Incorrect
The Go compiler checks interface satisfaction at compile time and raises an error if methods are missing.
Which of these is NOT true about Go interfaces?
AInterfaces specify method implementations
BInterfaces are satisfied implicitly
CInterfaces enable polymorphism
DInterfaces can hold any type that implements their methods
✗ Incorrect
Interfaces specify method signatures, not implementations.
What keyword is used to define an interface in Go?
Ainterface
Btype
Cstruct
Dfunc
✗ Incorrect
The 'type' keyword is used to define interfaces, combined with the 'interface' keyword inside the definition.
Describe how you define and use an interface in Go with a simple example.
Think about how you declare an interface and how a struct can implement it without extra keywords.
You got /4 concepts.
Explain why interfaces are useful in Go programming and how they support flexible code design.
Consider how interfaces let different types behave similarly without sharing code.
You got /4 concepts.
Practice
(1/5)
1. What does an interface in Go define?
easy
A. A concrete type with fields and methods
B. A variable that holds data
C. A set of method signatures without implementation
D. A function that returns multiple values
Solution
Step 1: Understand what an interface is
An interface in Go specifies method signatures but does not provide their implementation.
Step 2: Differentiate from other types
Concrete types have fields and method implementations, unlike interfaces.
Final Answer:
A set of method signatures without implementation -> Option C
Quick Check:
Interface = method signatures only [OK]
Hint: Interfaces list methods only, no code inside [OK]
Common Mistakes:
Thinking interfaces hold data
Confusing interfaces with structs
Assuming interfaces implement methods
2. Which of the following is the correct syntax to define an interface named Shape with a method Area() returning a float64?
easy
A. func Shape interface { Area() float64 }
B. interface Shape { func Area() float64 }
C. type Shape struct { Area() float64 }
D. type Shape interface { Area() float64 }
Solution
Step 1: Recall Go interface syntax
Interfaces are defined with the keyword type, name, then interface and method signatures inside braces.
Step 2: Check each option
type Shape interface { Area() float64 } matches correct syntax. Options B, C, and D misuse keywords or structure.
Final Answer:
type Shape interface { Area() float64 } -> Option D
Hint: Use 'type Name interface { methods }' for interfaces [OK]
Common Mistakes:
Using 'interface' keyword alone without 'type'
Defining methods inside struct instead of interface
Placing 'func' keyword inside interface method
3. What will be the output of this Go code?
package main
import "fmt"
type Printer interface {
Print()
}
type Document struct {
content string
}
func (d Document) Print() {
fmt.Println(d.content)
}
func main() {
var p Printer
d := Document{"Hello, Go!"}
p = d
p.Print()
}
medium
A. Runtime panic: nil pointer dereference
B. Hello, Go!
C. Compilation error: cannot assign Document to Printer
D. No output
Solution
Step 1: Check interface implementation
Document has a method Print() matching Printer interface, so Document implements Printer.
Step 2: Assign and call method
Variable p of type Printer is assigned d (Document), then p.Print() calls Document.Print(), printing content.
Final Answer:
Hello, Go! -> Option B
Quick Check:
Interface method calls concrete method [OK]
Hint: Assign concrete type with matching methods to interface [OK]