0
0
Goprogramming~5 mins

Interface definition in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an interface in Go?
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.<br><br>Example:<br>
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() }
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'
What error occurs if a type does not implement all interface methods?
ANo error, it works fine
BRuntime panic
CCompile-time error
DLinker error
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
What keyword is used to define an interface in Go?
Ainterface
Btype
Cstruct
Dfunc
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.