Describe how you would implement composition over inheritance in Go using structs.

  • Embedding the parent struct within the child struct.
  • Extending the parent struct directly.
  • Using interfaces to achieve inheritance.
  • Combining methods of the parent and child structs.
In Go, composition over inheritance is typically achieved by embedding the parent struct within the child struct. This allows the child struct to inherit the fields and methods of the parent struct, promoting code reuse without creating tight coupling between the two. Composition provides more flexibility and avoids some of the issues associated with deep inheritance hierarchies.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *