▸ Embedding! ▸ What is it, and how does it work? ▸ More accurately, a lot of ways it does not work. ▸ Useful ways to apply embedding for fun and profit!* ▸ *Neither fun nor profit guaranteed. ▸ Sweet pictures of my awesome dog!
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 github.com/StabbyCutyou/embeddingtalk
▸ It does not establish nor imply a “base class / sub 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 github.com/StabbyCutyou/embeddingtalk
More than it used to! ▸ “A field declared with a type but no explicit field name is an embedded field. An embedded field 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. github.com/StabbyCutyou/embeddingtalk
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 github.com/StabbyCutyou/embeddingtalk
of all available methods with a receiver of type T. ▸ For an interface, the interface is it’s own MethodSet ▸ Keyword: Set github.com/StabbyCutyou/embeddingtalk
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 github.com/StabbyCutyou/embeddingtalk
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! github.com/StabbyCutyou/embeddingtalk
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! ▸ https://github.com/golang/go/issues/4876 ▸ It’s currently managed via a comment in the standard lib ▸ “Broken as intended” github.com/StabbyCutyou/embeddingtalk
it ▸ Embed abstract behaviors instead of concrete behaviors ▸ Don’t panic! ▸ What about embedding 2 or more interfaces… ▸ With duplicate methods? github.com/StabbyCutyou/embeddingtalk
receive data for embedded types ▸ Careful! With go 1.10, un-exported embedded types changed! ▸ You can add tags to embedded types ▸ Careful! This changes the behavior of marshaling! ▸ You can override and block out the marshaling behavior of embedded types ▸ Lots of examples, but little time to cover them all ▸ Let’s cover 1 specific case… github.com/StabbyCutyou/embeddingtalk
ViewModel ▸ Use promotion semantics to hide private information ▸ Watch out for tricky marshal and unmarshal behaviors w/r/t tags on embedded fields ▸ The shape of raw data also matters when combined with embedded tags ▸ See “7.marshaling/tags” of the examples on github ▸ TL;DR; ▸ Don’t rely on “-“ to ignore embedded secrets ▸ Redefine the field, use the same struct tag name, and include “omitempty” or else! github.com/StabbyCutyou/embeddingtalk
different ▸ The spec has been updated to include more context around embedding recently ▸ Selectors and MethodSets are pretty simple ▸ Promotion is where it gets tricky ▸ You can embed interfaces, but it gets weird github.com/StabbyCutyou/embeddingtalk
to their original receiver ▸ Once you go “down”, you can never go back up - only “sideways” or further “down” ▸ Once you embed a value, don’t touch it again ▸ Only interact with embedded types via the promotion exposed by their embedding type ▸ Or, throw caution to the wind ▸ I’m not the Go police github.com/StabbyCutyou/embeddingtalk