Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I could not find the proverb "Accept Interfaces Return Struct in Go" here #37

Open
cyantarek opened this issue Jan 6, 2020 · 4 comments

Comments

@cyantarek
Copy link

No description provided.

@robpike
Copy link
Collaborator

robpike commented Jan 6, 2020

I'm not a great fan of that one. It's subtle and tricky to explain compactly. And although popular, it's not often easy to apply well. Plus: Why structs? There are many other concrete types in Go.

@cyantarek
Copy link
Author

Then this can be "Accept Interfaces Return Concrete Types"

@robpike
Copy link
Collaborator

robpike commented Jan 6, 2020

I still don't favor this one. It's very hard to be clear about where it applies, and it often doesn't.

@invidian
Copy link

invidian commented Oct 6, 2021

It seems to me that one example where this does not apply is when you want to make user use a constructor function, rather than making the struct exported. If you return unexported struct, the function won't be picked up by documentation tool and returning an exported struct may lead to mis-use (e.g. constructing struct "by hand", then calling their methods, without proper initialization). This seems like a practice done with defensive programming.

Accepting interfaces (or maybe defining interfaces on the receiver side) would still be worth mentioning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants