Go's structural typing system for interfaces creates implementation challenges despite its conceptual simplicity. The inability to add default methods to interfaces without breaking existing code leads to messy APIs like flag.Value. A workaround using struct embedding with unexported methods can simulate default implementations by requiring all interface implementors to embed a defaults struct. This technique allows adding new methods to interfaces while maintaining backward compatibility, though it has limitations and ideally should be a language feature.

10m read timeFrom mcyoung.xyz
Post cover image
Table of contents
flag.Value is a MessStruct EmbeddingsUsing DefaultsShouldn’t This Be a Language Feature?Conclusion
1 Comment

Sort: