Skip to content

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

Open

Description

@cyantarek
No description provided.

Activity

  1. robpike commented on Jan 6, 2020

    @robpike
    Collaborator

    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.

  2. cyantarek commented on Jan 6, 2020

    @cyantarek
    Author

    Then this can be "Accept Interfaces Return Concrete Types"

  3. robpike commented on Jan 6, 2020

    @robpike
    Collaborator

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

  4. invidian commented on Oct 6, 2021

    @invidian

    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.

  5. ibihim commented on Feb 13, 2025

    @ibihim

    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.

    References:

  6. grandstairs commented on Apr 30, 2025

    @grandstairs

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions