4 ms·
I use Go interfaces fairly often, not least for easy mocking for testing scenarios. Not having to explicitly declare what interface a struct is to implement has
by drakonka 4y ago
I use Go interfaces fairly often, not least for easy mocking for testing scenarios. Not having to explicitly declare what interface a struct is to implement has never been an issue for me. Modern IDEs provide this information (I prefer JetBrains Goland), so I never feel like I'm working "blind". And if I really feel the need to enforce a certain interface implementation at compile time I can do so:
`var _ InterfaceName = &StructName{}`
Having refactored applications that use interfaces, I've never found the interface usage to make it any more difficult or bug-prone personally. I tend to stick to the "accept interfaces, return structs" guideline, and that's worked pretty well for my use cases.
- _benj 4y ago+1 for goland, allows me to be very productive and with interfaces in particular it makes it very easy to jump from the interface to the implementation(s)
- yonisto 4y agohow does it work? there can be endless implementations and the IDE/Compiler doesn't know beforehand what satisfies the interface as oppose to other languages ?
- drakonka 4y agoThis answer by a GoLand team lead seems like a nice overview of how it detects interface implementations: https://stackoverflow.com/a/62386775 https://stackoverflow.com/a/62386775