You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
I could not find the proverb "Accept Interfaces Return Struct in Go" here #37
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.
Reacted by Marco Feltmann, Trevor N. Suarez, Christopher Hunter, Peter Boothe, Oleg Namaka, Andrey Bocharnikov and Mike Schinkel
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.
This is a legit proverb. It is a paradigm change, that is not obvious to grasp as no other language does it like Go does.
Go interfaces generally belong in the package that consumes values of the interface type, not a package that implements the interface type. The implementing package should return concrete (usually pointer or struct) types. That way, new methods can be added to implementations without requiring extensive refactoring. See GoTip #49: Accept Interfaces, Return Concrete Types for more details.
you want to make user use a constructor function, rather than making the struct exported
I've been burned by this practice so many times. The author of the library did not predict how their code could/would be extended.
This pattern often means the author will not allow me to access internal state (often another struct) and replace some instance with a decorated version or one with tighter timeouts and so I have to recreate the entire library over again. I'm glad the http library did not follow this pattern. I can't imagine the repeated pain of needing to replace the http.Transport or net.Dialer instances.
That's not to say it couldn't be used well, but I really appreciate how open (public?) Go tends to be without constantly programming defensively against consumers of your code to force them to only use libraries the one way the author intended.
Activity
robpike commentedon 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 commentedon Jan 6, 2020
Then this can be "Accept Interfaces Return Concrete Types"
robpike commentedon 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 commentedon 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.
remove ireturn linter
add ireturn to the list of ignored linters
ibihim commentedon Feb 13, 2025
This is a legit proverb. It is a paradigm change, that is not obvious to grasp as no other language does it like Go does.
References:
grandstairs commentedon Apr 30, 2025
I've been burned by this practice so many times. The author of the library did not predict how their code could/would be extended.
This pattern often means the author will not allow me to access internal state (often another struct) and replace some instance with a decorated version or one with tighter timeouts and so I have to recreate the entire library over again. I'm glad the http library did not follow this pattern. I can't imagine the repeated pain of needing to replace the http.Transport or net.Dialer instances.
That's not to say it couldn't be used well, but I really appreciate how open (public?) Go tends to be without constantly programming defensively against consumers of your code to force them to only use libraries the one way the author intended.