SlideShare a Scribd company logo
EMBEDDING IN GO
IT SURE IS WEIRD @STABBYCUTYOU
OBLIGATORY OPENING SLIDES
WHAT AM I HERE TO TALK ABOUT?
▸ Embedding!
▸ What is it, and how does it work?
▸ Useful ways to apply embedding for fun and profit!*
▸ *Neither fun nor profit guaranteed.
▸ A few ways you can’t use embedding, and why.
▸ Sweet pictures of my awesome dog!
GITHUB.COM/STABBYCUTYOU/EMBEDDINGTALK
EMBEDDING
IT’S LIKE INHERITANCE,
RIGHT?
EMBEDDING IS NOT BETTER THAN
“CLASSICAL” INHERITANCE. IT’S A DIFFERENT
TOOL FOR A DIFFERENT PROBLEM
Me
FUN QUOTE FOR YOU TO DISAGREE WITH
WHAT IS EMBEDDING?
DEFINING EMBEDDING AND COMPOSITION
▸ Embedding is a way to re-use existing structs and interfaces via composition
▸ Think more behavior over lineage
▸ Embedding to define structure is perfectly valid, though
▸ Sideways Inheritance
▸ Embed to define the API of a struct or interface
WHAT IS EMBEDDING?
HOW DOES IT DIFFER FROM “CLASSICAL” INHERITANCE?
▸ It does not set up or even imply a “base class / super class” relationship.
▸ “Is a” VS “Has a” style relationship
▸ Method dispatch mostly works as you think it would
▸ There are some interesting edge regarding “where” the method gets called
from
▸ The embedding type cannot be exchanged for the embedded type
▸ You can’t pass in a Corgi to a method which wants a Dog
WHAT IS EMBEDDING?
CLASSICAL INHERITANCE
▸ Kingdom - Animalia
▸ Phylum - Chordata
▸ Class - Mammalia
▸ Order - Carnivora
▸ Family - Canidae
▸ Genus - Canis
▸ Species - Canis Lupus
▸ Subspecies - Canis Lupus Familiaris
▸ Breed - Pembroke Welsh Corgi
WHAT IS EMBEDDING?
EMBEDDING
WHAT IS EMBEDDING?
EMBEDDING
WHAT IS EMBEDDING?
WELL, WHAT DOES THE SPEC SAY?
▸ Not a whole lot!
▸ “A field declared with a type but no explicit field name is an anonymous field, also called an
embedded field or an embedding of the type in the struct. An embedded type must be
specified as a type name T or as a pointer to a non-interface type name *T, and T itself may
not be a pointer type. The unqualified type name acts as the field name.”
▸ “An interface T may use a (possibly qualified) interface type name E in place of a method
specification. This is called embedding interface E in T; it adds all (exported and non-
exported) methods of E to the interface T. An interface type T may not embed itself or any
interface type that embeds T, recursively.”
▸ It defines a series of rules for determining how promotion works, but is light on examples.
WHAT IS EMBEDDING
POINTER TYPES?
WHAT IS EMBEDDING
POINTER TYPES!
WHAT IS EMBEDDING
WHAT ABOUT…
WHAT IS EMBEDDING
WHAT IF…
METHODSETS,
SELECTORS,
& PROMOTION OH MY
HOW DOES EMBEDDING WORK?
SELECTORS
▸ Selectors are expressions that denote
fields or methods available on an object
▸ “For a value x of type T or *T where T is
not a pointer or interface type, x.f denotes
the field or method at the shallowest
depth in T where there is such an f. If there
is not exactly one f with shallowest depth,
the selector expression is illegal.”
▸ Example: t.F is a simple selector
HOW DOES EMBEDDING WORK?
METHODSETS
▸ MethodSets are the collection of all
available methods with a receiver of
type T.
▸ For an interface, the interface is it’s own
MethodSet
▸ Keyword: Set
HOW DOES EMBEDDING WORK?
PROMOTION
▸ Promotion occurs when type E is embedded
into type T, such that a field or method on E
can be accessed at the shallowest depth of an
instance of T.
▸ If E has field or method X, and T embeds E, X
is promoted if T doesn’t already contain X
▸ Promoted methods and fields are called on
their original receiver
▸ If T already contains X, X is called on T, never E
HOW DOES EMBEDDING WORK
CREATING AND SETTING
PROMOTION COMMOTION
EMBEDDING MULTIPLE STRUCTS
▸ You can embed more than one type into another type
▸ You can even nest embeddings
▸ You cannot embed the same type more than once
▸ What happens when you embed multiple types that result in ambiguous
selectors?
▸ The compiler lets you do it!
▸ However, it does not let you invoke those selectors!
PROMOTION COMMOTION
EMBEDDING MULTIPLE TYPES
PROMOTION COMMOTION
EMBEDDING MULTIPLE TYPES
PROMOTION COMMOTION
EMBEDDING EXPORTED TYPES
▸ You can embed any exported type from any package
▸ You cannot embed types from different packages with the same name
▸ What happens to un-exported fields and methods from exported types?
▸ There is a long standing “bug” in the reflect package about this!
▸ It’s currently managed via a comment in the standard lib
▸ “Broken as intended”
PROMOTION COMMOTION
EMBEDDING INTERFACES IN STRUCTS
▸ You can do it
▸ Embed abstract behaviors instead of
concrete behaviors
▸ Don’t panic!
▸ What about embedding 2 or more
interfaces…
▸ With duplicate methods?
PROMOTION COMMOTION
EMBEDDING INTERFACES IN STRUCTS
PROMOTION COMMOTION
EMBEDDING INTERFACES IN STRUCTS
PROMOTION COMMOTION
EMBEDDING INTERFACES IN STRUCTS
PROMOTION COMMOTION
EMBEDDING INTERFACES IN STRUCTS
PROMOTION COMMOTION
MARSHALING AND UNMARSHMALING
▸ You can expose and receive data for embedded types
▸ You can add tags to embedded types
▸ This changes the behavior of marshaling, be careful
▸ You can override and block out the marshaling behavior of embedded types
▸ Lots of examples, but little time to cover them all
▸ TL;DR;
WAYS I HAVE USED
EMBEDDING
FOR BETTER, OR WORSE
FUN EXAMPLE TIME
VIEW MODELS
▸ Don’t send your database models over REST
▸ Don’t bind request data to them blindly either
▸ That’s a different talk
▸ Embed them in a ViewModel
▸ Use promotion semantics to hide private information
▸ Watch out for the marshal and unmarshal behaviors called out in the samples!
FUN EXAMPLE TIME
VIEW MODELS
FUN EXAMPLE TIME
EXTENDING GENERATED CODE
▸ If you produce generated code which contains type T
▸ You can product a write-once user-editable file which defines CustomT - (CT)
▸ Users can extend T however they see fit via CustomT and CustomT.go
▸ Re-generating the code should never touch CustomT.go, only T.go
▸ Unless the user wants to!
▸ Users can upgrade your code, while keeping their changes intact
NOT FUN EXAMPLE TIME
REMEMBER: PROMOTED METHODS ARE CALLED ON THEIR ORIGINAL RECEIVER
▸ Here is a thing I got really really wrong
▸ Ported code from Ruby to Go
▸ Expected embedding to work similar to inheritance since “It’s better™”
▸ Didn’t
▸ If T embeds E, promoted method M1 will always call M2 on E
▸ Even if T includes method M2
SUMMARY
THE LAST 40 SLIDES
CONDENSED TO 3
ROUNDUP
EMBEDDING
▸ It’s not better than inheritance
▸ It’s different
▸ The spec is light on details, examples
▸ Also refers to it by two names!
▸ Selectors and MethodSets are pretty simple
▸ Promotion is where it gets tricky
▸ You can embed interfaces, but it gets weird
ROUNDUP
EMBEDDING
▸ Don’t touch the embedded type, only the
embedding type
▸ Methods and Fields are always called relative
to their original receiver
▸ Once you go down the embedded receiver
chain, you can only go sideways, or further
down
▸ Lots of ways to use embedding, not just as a
replacement for inheritance
ROUNDUP
EMBEDDING
▸ Once you embed a value, don’t touch it
again
▸ Only interact with embedded types via the
promotion exposed by their embedding type
▸ It’s too easy to inadvertently use the wrong
value otherwise
▸ Or, throw caution to the wind
▸ I’m not the Go police
THANKS!
@STABBYCUTYOU

More Related Content

Recently uploaded

Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
obonagu
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
zwunae
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 

Recently uploaded (20)

Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Embedding: It sure is weird

  • 1. EMBEDDING IN GO IT SURE IS WEIRD @STABBYCUTYOU
  • 2. OBLIGATORY OPENING SLIDES WHAT AM I HERE TO TALK ABOUT? ▸ Embedding! ▸ What is it, and how does it work? ▸ Useful ways to apply embedding for fun and profit!* ▸ *Neither fun nor profit guaranteed. ▸ A few ways you can’t use embedding, and why. ▸ Sweet pictures of my awesome dog!
  • 5. EMBEDDING IS NOT BETTER THAN “CLASSICAL” INHERITANCE. IT’S A DIFFERENT TOOL FOR A DIFFERENT PROBLEM Me FUN QUOTE FOR YOU TO DISAGREE WITH
  • 6. WHAT IS EMBEDDING? DEFINING EMBEDDING AND COMPOSITION ▸ Embedding is a way to re-use existing structs and interfaces via composition ▸ Think more behavior over lineage ▸ Embedding to define structure is perfectly valid, though ▸ Sideways Inheritance ▸ Embed to define the API of a struct or interface
  • 7. WHAT IS EMBEDDING? HOW DOES IT DIFFER FROM “CLASSICAL” INHERITANCE? ▸ It does not set up or even imply a “base class / super class” relationship. ▸ “Is a” VS “Has a” style relationship ▸ Method dispatch mostly works as you think it would ▸ There are some interesting edge regarding “where” the method gets called from ▸ The embedding type cannot be exchanged for the embedded type ▸ You can’t pass in a Corgi to a method which wants a Dog
  • 8. WHAT IS EMBEDDING? CLASSICAL INHERITANCE ▸ Kingdom - Animalia ▸ Phylum - Chordata ▸ Class - Mammalia ▸ Order - Carnivora ▸ Family - Canidae ▸ Genus - Canis ▸ Species - Canis Lupus ▸ Subspecies - Canis Lupus Familiaris ▸ Breed - Pembroke Welsh Corgi
  • 11. WHAT IS EMBEDDING? WELL, WHAT DOES THE SPEC SAY? ▸ Not a whole lot! ▸ “A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct. An embedded type must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.” ▸ “An interface T may use a (possibly qualified) interface type name E in place of a method specification. This is called embedding interface E in T; it adds all (exported and non- exported) methods of E to the interface T. An interface type T may not embed itself or any interface type that embeds T, recursively.” ▸ It defines a series of rules for determining how promotion works, but is light on examples.
  • 17. HOW DOES EMBEDDING WORK? SELECTORS ▸ Selectors are expressions that denote fields or methods available on an object ▸ “For a value x of type T or *T where T is not a pointer or interface type, x.f denotes the field or method at the shallowest depth in T where there is such an f. If there is not exactly one f with shallowest depth, the selector expression is illegal.” ▸ Example: t.F is a simple selector
  • 18. HOW DOES EMBEDDING WORK? METHODSETS ▸ MethodSets are the collection of all available methods with a receiver of type T. ▸ For an interface, the interface is it’s own MethodSet ▸ Keyword: Set
  • 19. HOW DOES EMBEDDING WORK? PROMOTION ▸ Promotion occurs when type E is embedded into type T, such that a field or method on E can be accessed at the shallowest depth of an instance of T. ▸ If E has field or method X, and T embeds E, X is promoted if T doesn’t already contain X ▸ Promoted methods and fields are called on their original receiver ▸ If T already contains X, X is called on T, never E
  • 20. HOW DOES EMBEDDING WORK CREATING AND SETTING
  • 21. PROMOTION COMMOTION EMBEDDING MULTIPLE STRUCTS ▸ You can embed more than one type into another type ▸ You can even nest embeddings ▸ You cannot embed the same type more than once ▸ What happens when you embed multiple types that result in ambiguous selectors? ▸ The compiler lets you do it! ▸ However, it does not let you invoke those selectors!
  • 24. PROMOTION COMMOTION EMBEDDING EXPORTED TYPES ▸ You can embed any exported type from any package ▸ You cannot embed types from different packages with the same name ▸ What happens to un-exported fields and methods from exported types? ▸ There is a long standing “bug” in the reflect package about this! ▸ It’s currently managed via a comment in the standard lib ▸ “Broken as intended”
  • 25. PROMOTION COMMOTION EMBEDDING INTERFACES IN STRUCTS ▸ You can do it ▸ Embed abstract behaviors instead of concrete behaviors ▸ Don’t panic! ▸ What about embedding 2 or more interfaces… ▸ With duplicate methods?
  • 30. PROMOTION COMMOTION MARSHALING AND UNMARSHMALING ▸ You can expose and receive data for embedded types ▸ You can add tags to embedded types ▸ This changes the behavior of marshaling, be careful ▸ You can override and block out the marshaling behavior of embedded types ▸ Lots of examples, but little time to cover them all ▸ TL;DR;
  • 31. WAYS I HAVE USED EMBEDDING FOR BETTER, OR WORSE
  • 32. FUN EXAMPLE TIME VIEW MODELS ▸ Don’t send your database models over REST ▸ Don’t bind request data to them blindly either ▸ That’s a different talk ▸ Embed them in a ViewModel ▸ Use promotion semantics to hide private information ▸ Watch out for the marshal and unmarshal behaviors called out in the samples!
  • 34. FUN EXAMPLE TIME EXTENDING GENERATED CODE ▸ If you produce generated code which contains type T ▸ You can product a write-once user-editable file which defines CustomT - (CT) ▸ Users can extend T however they see fit via CustomT and CustomT.go ▸ Re-generating the code should never touch CustomT.go, only T.go ▸ Unless the user wants to! ▸ Users can upgrade your code, while keeping their changes intact
  • 35. NOT FUN EXAMPLE TIME REMEMBER: PROMOTED METHODS ARE CALLED ON THEIR ORIGINAL RECEIVER ▸ Here is a thing I got really really wrong ▸ Ported code from Ruby to Go ▸ Expected embedding to work similar to inheritance since “It’s better™” ▸ Didn’t ▸ If T embeds E, promoted method M1 will always call M2 on E ▸ Even if T includes method M2
  • 36. SUMMARY THE LAST 40 SLIDES CONDENSED TO 3
  • 37. ROUNDUP EMBEDDING ▸ It’s not better than inheritance ▸ It’s different ▸ The spec is light on details, examples ▸ Also refers to it by two names! ▸ Selectors and MethodSets are pretty simple ▸ Promotion is where it gets tricky ▸ You can embed interfaces, but it gets weird
  • 38. ROUNDUP EMBEDDING ▸ Don’t touch the embedded type, only the embedding type ▸ Methods and Fields are always called relative to their original receiver ▸ Once you go down the embedded receiver chain, you can only go sideways, or further down ▸ Lots of ways to use embedding, not just as a replacement for inheritance
  • 39. ROUNDUP EMBEDDING ▸ Once you embed a value, don’t touch it again ▸ Only interact with embedded types via the promotion exposed by their embedding type ▸ It’s too easy to inadvertently use the wrong value otherwise ▸ Or, throw caution to the wind ▸ I’m not the Go police