Interface Embedding
Compose interfaces.
Interface Embedding is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)
}io.ReadWriter in the Wild
The classic example is io.ReadWriter, defined as an embedding of io.Reader and io.Writer. Any type with both methods satisfies it.
Satisfying a Composed Interface
A concrete type implements the combined interface simply by having all the required methods.
package main
import "fmt"
type Reader interface{ Read() string }
type Writer interface{ Write(s string) }
type ReadWriter interface {
Reader
Writer
}
type File struct{ data string }
func (f *File) Read() string { return f.data }
func (f *File) Write(s string) { f.data = s }
func main() {
var rw ReadWriter = &File{}
rw.Write("hi")
fmt.Println(rw.Read())
}Why Compose Interfaces
Small interfaces are easy to implement and test. Composing them builds larger contracts only where needed, keeping each piece minimal.
Building From Small Parts
Define focused single-method interfaces, then combine.
package main
import "fmt"
type Opener interface{ Open() }
type Closer interface{ Close() }
type OpenCloser interface {
Opener
Closer
}
type Door struct{}
func (Door) Open() { fmt.Println("opened") }
func (Door) Close() { fmt.Println("closed") }
func main() {
var oc OpenCloser = Door{}
oc.Open()
oc.Close()
}No Duplicate Methods
Embedded interfaces must not contribute overlapping methods with conflicting signatures; identical method sets are fine in modern Go.
Accepting Narrow Interfaces
Functions should ask for the smallest interface they need. Take io.Reader, not io.ReadWriteCloser, if you only read.
package main
import "fmt"
type Reader interface{ Read() string }
func dump(r Reader) { fmt.Println(r.Read()) }
type Buf struct{}
func (Buf) Read() string { return "data" }
func main() {
dump(Buf{})
}The io Family
The standard library composes Reader, Writer, Closer into ReadCloser, WriteCloser, ReadWriteCloser — a clean lattice of contracts.
Composed Interfaces in APIs
Return the broadest interface a value supports, but accept the narrowest one your function uses. This maximizes both flexibility and clarity.
Interface vs Struct Embedding
- Struct embedding promotes fields and methods for reuse of behavior
- Interface embedding combines method requirements into a bigger contract
Quick Check
Test your interface embedding knowledge.
Recap
You learned interface embedding.
- List interfaces inside another to merge method sets
- The io package composes Reader/Writer/Closer
- Keep base interfaces small; compose where needed
- Accept the narrowest interface a function requires
Frequently asked questions
Is the “Interface Embedding” lesson free?
Yes — the full text of “Interface Embedding” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Interface Embedding”?
Compose interfaces. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Interface Embedding” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Struct Embedding
- Interface Embedding
- Method Promotion
- Composition over Inheritance