You are designing a Go application to model a car dealership inventory. Explain how you would use structs to represent different types of vehicles in the inventory.

  • Use a base struct 'Vehicle' with common attributes like 'Make,' 'Model,' 'Year,' and 'Price.' Then, create specific vehicle structs like 'Car' and 'Motorcycle' that embed the 'Vehicle' struct and add unique attributes like 'NumberOfDoors' for cars and 'EngineType' for motorcycles. This way, you can reuse common attributes while extending them for specific vehicle types, making the code more maintainable and efficient.
  • Use separate structs for each vehicle type, such as 'Car' and 'Motorcycle,' with their unique attributes. Avoid using a base 'Vehicle' struct to keep the code cleaner and more straightforward.
  • Create a single 'Vehicle' struct with all possible attributes, including those specific to cars and motorcycles. This approach simplifies the code structure but may lead to confusion and increased maintenance efforts as the application grows.
  • Define separate interfaces for 'Car' and 'Motorcycle' and implement them in respective structs. This provides flexibility but can be complex and less efficient.
Using a base struct ('Vehicle') with common attributes and embedding it in specific vehicle structs ('Car' and 'Motorcycle') is a beneficial approach. It promotes code reusability and maintainability by avoiding redundancy and allowing you to extend common attributes while keeping the code organized.
Add your answer
Loading...

Leave a comment

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