4 ms·
#8 is pretty great in that you can assign methods to custom types but not builtins. As an example, say I have []Card. If I want to sum all cards with a certain
by Everlag 11y ago
#8 is pretty great in that you can assign methods to custom types but not builtins.
As an example, say I have []Card. If I want to sum all cards with a certain color then I could
Sum(cards []Card, color string)
but that is not great. Rather, I could use
type Cards []Card
and then name a method
(cards *Cards) Sum(color string) int
The benefit is that any []Card can now have Sum called on it without any explicit casting.
I've been using this and find it to be clean.