Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I often find it useful that implementing an interface is implicit. For example maybe I'm using a library that provides a struct A. I can make another struct B that mocks that functionality for testing, and then everywhere in my code use an interface C that encapsulates A and B. That wouldn't be possible if A had to explicitly declare that it implements C.

If you want to explicitly declare that you implement an interface.. just put it in the comments, or it's common to see a line like this after a struct declaration:

  var _ C = (*A)(nil);
It's a way of asserting that A satisfies C.


- that's an optional convention, and it is not always followed

- not easy to search for this pattern.

You are right that go interfaces give you some flexibility with mocking, but i still think it's a questionable choice.


If the problem is finding implementations of an interface, that's provided by the standard tooling. If you're using VSCode just right-click on an interface and select Find All Implementations.


actually i was not using 'standard tooling'; was using vim with YouCompleteMe plugin for code completion. I couldn't get all the VSCode plugins to work well, somehow.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: