Hacker Newsnew | past | comments | ask | show | jobs | submit | richardsondrew's commentslogin

> However, the issue this brings up about structs / types not explicitly declaring which interfaces they implement is a real and unaddressed problem

You can do this with a line of code below the struct definition, something like:

  var _ <interface> = &<struct>{}
The compiler will also generate helpful errors if the struct doesn't implement the interface.

Not requiring struct definition is a great feature in golang. I'm able to add an interface to a struct defined in another library to inject a different implementation for unit tests.


It would be cool though to be able to optionally declare the interfaces implemented by your struct, for documentation and tooling purpose. Also to validate that the struct you’re writing actually implements the interface you have in mind. I know you can make an anonymous assignment to check that, but some sugar on that would be nice.


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

Search: