3 ms·
Struct embedding has been mostly unpleasant in my experience. When a type is embedded, only the methods that are explicitly on the interface/struct actually ge
by morecoffee 10y ago
Struct embedding has been mostly unpleasant in my experience. When a type is embedded, only the methods that are explicitly on the interface/struct actually get propagated, rather than all of public methods. This means that wrapper types break optimizations around type punning. For example: https://play.golang.org/p/vIo1HtAeb4 https://play.golang.org/p/vIo1HtAeb4
This happens within the standard library in ways that hurt performance. The 'image' package allows people to register their own decoders but limits the API to an io.Reader. It can be an optimization if you could cast the inputted reader to another more efficient type (like a ReaderAt), but you can't because the image package wraps the Reader in its own "peeking" type. There's no way to tell the type was embedded, or what other actual methods might have worked.