3 ms·
var _ MyInterface = &MyStruct{} Now your compiler will tell you you stopped implementing the interface. Pretty? No. But it works. And gopls will even offer to
by avanai 5mo ago
var _ MyInterface = &MyStruct{}
Now your compiler will tell you you stopped implementing the interface. Pretty? No. But it works. And gopls will even offer to implement stubs for missing methods.
- BadBadJellyBean 5mo agoThat is helpful but I think that is still a fail for Go. This could and should be a simple annotation.
- pdmccormick 5mo agoThis also works: var _ MyInterface = (*MyStruct)(nil) In my mind as I read this, I am reminded that it is the receiver type *MyStruct that implements MyInterface. YMMV.
- BadBadJellyBean 5mo agoThis is a useful hack but just imagine someone coming from another language reading this. They'd have no idea what it's for. It looks a bit like the things C people do to themselves. And all just because someone didn't want a keyword or another form of annotation to signal that a struct should implement an interface. These are the small things that annoy me about go.