0PricingLogin
Go Academy · Lesson

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

  1. Struct Embedding
  2. Interface Embedding
  3. Method Promotion
  4. Composition over Inheritance
← Back to Go Academy