3 ms·
Thanks for this - I've been through the golang wiki tutorial, but this (ab)use of an empty interface for storing "any"-type in a collection was a great idiom to
by e12e 14y ago
Thanks for this - I've been through the golang wiki tutorial, but this (ab)use of an empty interface for storing "any"-type in a collection was a great idiom to be aware of.
- evmar 14y agoAnother way to look at it is that an interface wraps a type by adding a pointer to an extra data structure, which identifies the type and functions on the type that implement the interface. Since interface{} has no functions, all that it carries is the type; it's concretely represented as a pair of the value and its type. Which is more or less how any language implements an "any" type. (Disclaimer: it still feels awkward to me, but I found understanding the implementation of it helped me understand the why better. See also http://research.swtch.com/interfaces http://research.swtch.com/interfaces .)