Interfaces let us write flexible code by defining what methods a type must have. Interface satisfaction means a type has all methods an interface needs, so it can be used wherever that interface is expected.
Interface satisfaction in Go
type InterfaceName interface { Method1(params) returnType Method2(params) returnType } // A type satisfies an interface if it has all methods listed in the interface. type MyType struct {} func (m MyType) Method1(params) returnType { // implementation } func (m MyType) Method2(params) returnType { // implementation }
In Go, you don't explicitly declare that a type implements an interface. It happens automatically if the type has all required methods.
This is called 'implicit interface satisfaction' and helps keep code simple and flexible.
Dog satisfies the Speaker interface because it has the Speak method.type Speaker interface { Speak() string } type Dog struct {} func (d Dog) Speak() string { return "Woof!" }
File satisfies the Reader interface by implementing the Read method.type Reader interface { Read(p []byte) (n int, err error) } type File struct {} func (f File) Read(p []byte) (int, error) { // read data into p return 0, nil }
This program shows a Person type that satisfies the Greeter interface by having a Greet method. The sayHello function accepts any Greeter. We pass a Person to it, and it works because Person satisfies the interface.
package main import "fmt" type Greeter interface { Greet() string } type Person struct { name string } func (p Person) Greet() string { return "Hello, " + p.name } func sayHello(g Greeter) { fmt.Println(g.Greet()) } func main() { p := Person{name: "Alice"} sayHello(p) // Person satisfies Greeter interface }
Interface satisfaction is checked automatically by the Go compiler.
If a type misses even one method from the interface, it does not satisfy it.
You can use interfaces to write more testable and flexible code.
Interfaces define a set of methods a type must have.
A type satisfies an interface by implementing all its methods.
Go uses implicit interface satisfaction, so no extra declaration is needed.
Practice
Solution
Step 1: Understand interface satisfaction in Go
In Go, a type satisfies an interface if it implements all the methods declared in that interface.Step 2: Check explicit declaration requirement
Go does not require explicit declaration; satisfaction is implicit by method implementation.Final Answer:
A type satisfies an interface by implementing all the methods declared in the interface. -> Option AQuick Check:
Interface satisfaction = implicit method implementation [OK]
- Thinking explicit declaration is required
- Confusing embedding with satisfaction
- Matching names instead of methods
Speaker with method Speak()?Solution
Step 1: Check method signature and receiver
type Speaker interface { Speak() string } type Dog struct {} func (d Dog) Speak() string { return "Woof" } definesSpeak()with correct receiver(d Dog)and return typestring.Step 2: Verify method name and return type match interface
type Speaker interface { Speak() string } type Dog struct {} func (d Dog) speak() string { return "Woof" } has lowercasespeak(), so it does not satisfy interface. type Speaker interface { Speak() string } type Dog struct {} func Speak(d Dog) string { return "Woof" } defines a function, not a method. type Speaker interface { Speak() string } type Dog struct {} func (d *Dog) Speak() int { return 1 } has wrong return typeint.Final Answer:
type Speaker interface { Speak() string } type Dog struct {} func (d Dog) Speak() string { return "Woof" } -> Option BQuick Check:
Method name and signature must match exactly [OK]
- Using lowercase method names
- Defining functions instead of methods
- Mismatching return types
package main
import "fmt"
type Reader interface {
Read() string
}
type File struct {}
func (f File) Read() string {
return "file content"
}
func printContent(r Reader) {
fmt.Println(r.Read())
}
func main() {
f := File{}
printContent(f)
}Solution
Step 1: Check if File satisfies Reader interface
File has methodRead() string, so it satisfies Reader.Step 2: Trace function call and output
Inmain,fis passed toprintContent, which callsRead()and prints "file content".Final Answer:
file content -> Option CQuick Check:
Interface satisfied, method called, prints string [OK]
- Assuming explicit declaration needed
- Confusing method receiver types
- Expecting runtime errors without cause
Car from satisfying the Vehicle interface:package main
import "fmt"
type Vehicle interface {
Drive()
}
type Car struct {}
func (c *Car) Drive() {
fmt.Println("Driving")
}
func main() {
var v Vehicle
c := Car{}
v = c
v.Drive()
}Solution
Step 1: Check method receiver type
Drive is defined with pointer receiver(*Car), so only *Car satisfies Vehicle, not Car value.Step 2: Check assignment in main
Variablevis assignedcwhich is a value, not pointer, causing mismatch.Final Answer:
Car does not satisfy Vehicle because Drive has pointer receiver but v is assigned a value. -> Option AQuick Check:
Pointer receiver methods require pointer type for interface satisfaction [OK]
- Assigning value instead of pointer to interface
- Ignoring receiver type in method
- Assuming method signature mismatch
type Writer interface {
Write(data string) error
}
type Logger struct {}
func (l Logger) Write(data string) error {
return nil
}
type FileWriter struct {}
func (f *FileWriter) Write(data string) error {
return nil
}
var w Writer
// Which assignment is invalid?
// A) w = Logger{}
// B) w = &Logger{}
// C) w = FileWriter{}
// D) w = &FileWriter{}Solution
Step 1: Analyze Logger methods
Logger hasWritewith value receiver, so both Logger and *Logger satisfy Writer.Step 2: Analyze FileWriter methods
FileWriter hasWritewith pointer receiver, so only *FileWriter satisfies Writer, not FileWriter value.Step 3: Check assignments
Assignment C tries to assign FileWriter{} value to Writer, which is invalid.Final Answer:
w = FileWriter{} causes compile-time error due to missing method on value receiver. -> Option DQuick Check:
Pointer receiver methods require pointer type for interface [OK]
- Assigning value type when method has pointer receiver
- Assuming pointer and value types are interchangeable
- Ignoring method receiver type
Ready to prove it?
Pass the test and this pattern moves unknown
