SlideShare ist ein Scribd-Unternehmen logo
1 von 12
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 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).
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
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
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
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:
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
Real-ish World Example : PubCloud
https://github.com/m4tty/pubcloud
An over abstracted example, in order to highlight interface usage.
Questions?
So long and thanks for all the fish!

Weitere ähnliche Inhalte

Ähnlich wie Interfaces in Go

Advanced
AdvancedAdvanced
Advanced
mxmxm
 
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
HostedbyConfluent
 

Ähnlich wie Interfaces in Go (20)

Lambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeLambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive Code
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
Advanced
AdvancedAdvanced
Advanced
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
NLJUG University Sessie: Java Reborn, Powered by Ordina
NLJUG University Sessie: Java Reborn, Powered by OrdinaNLJUG University Sessie: Java Reborn, Powered by Ordina
NLJUG University Sessie: Java Reborn, Powered by Ordina
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to Gophers
 
Using Pony for Fintech
Using Pony for FintechUsing Pony for Fintech
Using Pony for Fintech
 
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
 
JavaZone 2014 - goto java;
JavaZone 2014 - goto java;JavaZone 2014 - goto java;
JavaZone 2014 - goto java;
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
 
Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scala
 
Andriy Shalaenko - GO security tips
Andriy Shalaenko - GO security tipsAndriy Shalaenko - GO security tips
Andriy Shalaenko - GO security tips
 
Groovy AST Transformations
Groovy AST TransformationsGroovy AST Transformations
Groovy AST Transformations
 
How To Use IO Monads in Scala?
 How To Use IO Monads in Scala? How To Use IO Monads in Scala?
How To Use IO Monads in Scala?
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
 
ParaSail
ParaSail  ParaSail
ParaSail
 
The Rust Programming Language
The Rust Programming LanguageThe Rust Programming Language
The Rust Programming Language
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 

Kürzlich hochgeladen

Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Kürzlich hochgeladen (20)

Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 

Interfaces in Go

  • 3. "We demand rigidly defined areas of doubt and uncertainty!" - The Hitchhiker’s Guide to the Galaxy
  • 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. 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. 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. 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. 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. 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. Real-ish World Example : PubCloud https://github.com/m4tty/pubcloud An over abstracted example, in order to highlight interface usage.
  • 12. So long and thanks for all the fish!