Interface Embedding
Compose interfaces.
Compose Interfaces
Interfaces can embed other interfaces, combining their method sets into a larger contract. The standard library uses this everywhere.
Embedding Syntax
List an interface type inside another interface to include all its methods.
package main
import "fmt"
type Reader interface{ Read() string }
type Writer interface{ Write(s string) }
type ReadWriter interface {
Reader
Writer
}
func main() {
var rw ReadWriter
fmt.Println(rw == nil)
}All lessons in this course
- Struct Embedding
- Interface Embedding
- Method Promotion
- Composition over Inheritance