SlideShare une entreprise Scribd logo
1  sur  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!

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

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!