The Hidden Power of Humble
Interfaces
Aditya Mukerjee
@ChimeraCoder
GopherCon Singapore May 26, 2017
Slide 2
Slide 2 text
@chimeracoder
Slide 3
Slide 3 text
No content
Slide 4
Slide 4 text
No content
Slide 5
Slide 5 text
Why use interfaces?
• Write less code
• Write robust code
• Write flexible code
@chimeracoder
Slide 6
Slide 6 text
Interfaces: The Basics
Slide 7
Slide 7 text
@chimeracoder
Slide 8
Slide 8 text
io.* interfaces
@chimeracoder
Slide 9
Slide 9 text
Composite interfaces
@chimeracoder
Slide 10
Slide 10 text
Interface helper functions
@chimeracoder
Slide 11
Slide 11 text
What makes the io interfaces powerful?
• Abstracting a lot of common functionality
• Lots of granularity
• Plethora of helper functions
@chimeracoder
Slide 12
Slide 12 text
What makes error powerful?
• Abstracts no functionality
• Provides no granularity
• Provides (almost) no helper functions
@chimeracoder
Slide 13
Slide 13 text
So what makes a powerful interface, anyway?
@chimeracoder
Slide 14
Slide 14 text
Humility and Discipline
• Keep interfaces humble
• Writing interfaces forces you to define the minimum required contract for
using your types
• Keep interfaces disciplined
• Writing interfaces allows the compiler to enforce this contract
@chimeracoder
Slide 15
Slide 15 text
type Foo struct interface {
}
@chimeracoder
Slide 16
Slide 16 text
Why Gophers avoid interfaces
• Afraid of writing an interface that is too complicated
• Afraid of specifying the wrong methods
@chimeracoder
Slide 17
Slide 17 text
How I Learned To Stop Worrying and Love the interface
• Interfaces reveal the assumptions you’re already making
• “If it’s there, you will use it”
@chimeracoder
Slide 18
Slide 18 text
Every time you access a field or method,
you’re making an assumption
@chimeracoder
Slide 19
Slide 19 text
So how should I design these interfaces?
@chimeracoder
Slide 20
Slide 20 text
Writing interfaces like io.*
• Don’t write helper methods until you actually find you need them
• The contracts provided by each should be minimal
• Create composite interface types
@chimeracoder
Slide 21
Slide 21 text
Writing interfaces like error
• Ask interfaces how they behave
@chimeracoder
Slide 22
Slide 22 text
What if structs could never be exported?
@chimeracoder