Better HN
topnewbestaskshowjobs
Log inSign up

richardsondrew

16 karma · joined Jun 14, 2022 (4y ago) · 1 submissions

Submissions

comment 4y ago · link · parent

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