Hacker News new | ask | show | jobs
by avanai 41 days 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.
2 comments

This 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.
This 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.

That is helpful but I think that is still a fail for Go. This could and should be a simple annotation.