Interfaces in Go
Preparing for Change
Matt Self
@_m4tty
github.com/m4tty
"We demand rigidly defined areas of doubt and uncertainty!"
- The Hitchhiker’s Guide to the Galaxy
What is an Interface?
An access view that separates the What from the How
The What is our defined entry point

The How is ...
Protected Variation
“Identify points of predicted variation and create a stable interface around
them.” - Alistair Cockbur...
Interfaces in Go?
A custom type that specifies a group of method signatures
Checked at compile time, but dynamic when need...
And what is this interface{} thing?
The interface{} type is a interface that specifies no methods
type interface interface...
Interface values
Runtime computed (and cached) method tables
interface table (Animal, Gopher)
type

Gopher

fn[0]

(Gopher...
Embedding
Embedding interfaces
type Animal interface {
Run()

}
type Monkey interface {
Animal

Swing()
}

Embedding types...
Real-ish World Example : PubCloud
https://github.com/m4tty/pubcloud
An over abstracted example, in order to highlight inte...
Questions?
So long and thanks for all the fish!
Upcoming SlideShare
Loading in …5
×

Interfaces in Go

921 views
683 views

Published on

Interfaces in Go (Golang): Preparing for Change. Presented at Denver Gopher Meetup on 2.27.14.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
921
On SlideShare
0
From Embeds
0
Number of Embeds
55
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Interfaces in Go

  1. 1. Interfaces in Go Preparing for Change
  2. 2. Matt Self @_m4tty github.com/m4tty
  3. 3. "We demand rigidly defined areas of doubt and uncertainty!" - The Hitchhiker’s Guide to the Galaxy
  4. 4. What is an Interface? An access view that separates the What from the How The What is our defined entry point The How is the details that we wish to hide We hide our internal implementation (the How) by allowing access only through a well defined entry point (the What).
  5. 5. Protected Variation “Identify points of predicted variation and create a stable interface around them.” - Alistair Cockburn “We propose instead that one begins with a list of difficult design decisions or design decisions which are likely to change.” - David Parnas Craig Larman - Protected Variation: The Importance of Being Closed http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf David Parnas - On Criteria to Be Used in Decomposing Systems into Modules https://www.cs.umd.edu/class/spring2003/cmsc838p/Design/criteria.pdf
  6. 6. Interfaces in Go? A custom type that specifies a group of method signatures Checked at compile time, but dynamic when needed Duck typing. No “implements” needed. Allows for emerging design. type Animal interface { Run() } http://play.golang.org/p/QG3uEn30EL
  7. 7. And what is this interface{} thing? The interface{} type is a interface that specifies no methods type interface interface {} This type can be used for any value, because all values meet this interface. Remember “dynamic when needed”... http://play.golang.org/p/ERrw8A9614
  8. 8. Interface values Runtime computed (and cached) method tables interface table (Animal, Gopher) type Gopher fn[0] (Gopher).Run var a Animal = Gopher{} A function pointer to gopher Run a.table a.data Gopher data Calling a.Run() causes Go compiler to generate code that does: a.table -> fn[0] Checking the type (i.e. v, a.table -> type ok := a.(type) ) does:
  9. 9. Embedding Embedding interfaces type Animal interface { Run() } type Monkey interface { Animal Swing() } Embedding types, or “delegating” will meet interface specification if embedded type meets specification. http://play.golang.org/p/T6xXHFls8s
  10. 10. Real-ish World Example : PubCloud https://github.com/m4tty/pubcloud An over abstracted example, in order to highlight interface usage.
  11. 11. Questions?
  12. 12. So long and thanks for all the fish!

×