4 ms·
Overall this is pretty solid advice, with only two things I disagreed with a bit: > Go is still a new language Go is between 12 and 17 years old, depending on
by silisili 2y ago
Overall this is pretty solid advice, with only two things I disagreed with a bit:
> Go is still a new language
Go is between 12 and 17 years old, depending on how you look at it. It's not as old as the big players, but it's far from new.
> Do not think returning an interface is a bad idea
It's almost always a bad idea to return an interface. Return a type that implements said interface(s).
If you want to hide away details, use lowercase to not export them. This section had a solid point, then backtracked a little, and I wish it hadn't.
- DSingularity 2y agoHe wrote an entire section on why you shouldn’t return an interface and then gave a concrete example of an exception to the rule. It seems to me that it is indeed a valid exception.
- silisili 2y agoI don't disagree with the specific example given, but rather that because those cases exist, we blanket say it's not a bad idea to do so...if that makes sense. It's why I used 'almost' there. Perhaps it needed to be a little segregated. The initial section is written from the perspective of a library author, asking them not to nuke away types. The example seems more of a middleware/client side thing. More often than not of course it'll make more sense to deal with types at the base layer, and interfaces at the client layers, as that's the point of them, after all.
- kazinator 2y agoBut that's clearly in a section which poses interface returning as one of the interface mistake: 5. You are returning interfaces The author just backpedals little; it's not absolutely wrong to return an interface.