What Is the Extension Interface Pattern in Go?

Emmanuel Sys
The Startup
Published in
5 min readAug 7, 2020

--

A recent video by Russ Cox and Rob Pike officially coined the term “Extension interface pattern” in Go. What are we talking about?

Go is not a fully featured object-oriented language. Instead, it offers a subset of OOP concepts. For example, you can attach a method to any type or encapsulate data through the use of internal or public (exported) identifier.

In the following exemple, Person has both a public and private fields and a method attached.

type Person struct {
ID string // public as it starts uppercase
name string // private
}

func (p Person) sayHello(){
fmt.Printf("Hello %s", p.name)
}

func main(){
p := Person{ID: "0", name: "John"}
p.sayHello()
}

So we’ve got both encapsulation and methods covered. But one often used feature of OOP is the capacity to extend a class through inheritance. In fact, you could even say it defined object programming for a lot of people.

What if you want to do something like inheritance in Go ?

Back to basics : why using inheritance ?

The main motivation to use inheritance boils down to 2 use cases:

  • Code reuse: you have a bunch of code you want to reuse between related classes

--

--

Emmanuel Sys
The Startup

Passionate about software and cloud architecture ☁️ I like building apps, deploying them and breaking things 🤗