You're designing a system where you need to represent various shapes. How would you use structs in Go to model this?

  • Define a struct for each shape, such as Rectangle, Circle, Triangle, etc., with fields representing their attributes like width, height, radius, etc.
  • Use a map with string keys representing shape names and interface{} values representing shape attributes, allowing for dynamic addition of new shapes.
  • Use a single struct named Shape with a field to identify the type of shape and additional fields representing attributes, which might result in a less clear and more complex design.
  • Use an interface named Shape with methods like Area() and Perimeter() and then define separate structs like Rectangle, Circle, Triangle implementing this interface.
Defining a struct for each shape with specific fields provides a clear and structured way to represent different shapes in the system, facilitating ease of use and maintenance. It follows the principle of composition, where each struct represents a distinct entity with its own attributes and behaviors.
Add your answer
Loading...

Leave a comment

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