3 ms·
> However quite a few Go developers now use interfaces with an unexported method specifically to stop users from misusing their types in a context they didn't i
by artursapek 8y ago
> However quite a few Go developers now use interfaces with an unexported method specifically to stop users from misusing their types in a context they didn't intend. I guess it's come full circle.
What a horrible hack. Why bother with that? Either export your interface, your don't.
- cyphar 8y agoWell -- what if you have multiple implementations of an interface and you want users to be able to choose between them (or rather, you want them to be able to write functions around your interfaces)? I don't really like it much either, but it's a completely valid usecase for unexported methods. In fact I'm pretty sure this was an example from the language developers for why you can have unexported interface methods in Go.
- burntsushi 8y ago> In fact I'm pretty sure this was an example from the language developers for why you can have unexported interface methods in Go. Indeed. It's used in the standard library for describing Go's AST if I recall correctly.