you are viewing a single comment's thread
view the rest of the comments
[–] 4 points 3 years ago (2 children)

You're not describing composition.

Go Files do not "hasa reader". You don't do file.reader.read(), you just do file.read(), that's inheritance as file has inherited the read() method.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 8 points 3 years ago

    You're confusing polymorphism for inheritance. read is a method on an interface that File implements - it is not inherited from a base class. You can use that File directly, or wherever a Reader interface (or whatever the name is, idk I don't really do Go) is expected.

  • source
  • parent
  • [–] 4 points 3 years ago (1 child)

    Composition do not necessitate the creation of a new field like x.reader or x.writer, what are you on?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 0 points 3 years ago (1 child)
  • [–] 1 point 3 years ago* (last edited 3 years ago)

    Man, I honestly have no idea why they are downvoting you. Composition literally means taking common behavior and placing it in external objects behind interfaces using a has-a relationship.

    No idea why they are denying this. Inheritance vs composition is the same as "is-a" vs "has-a". In composition re usability isn't done via inheritance but code encapsulation.

    Saying that in Go objects can implement many interfaces is the Interface Segregation principle from SOLID. Basically having small interfaces instead of a big interface with methods that may not be implemented by all implementors.

  • source
  • parent