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.
Loading...
Related Quiz
- How does the sync.WaitGroup type help in managing a collection of Goroutines?
- The _____ command is used to initialize a new module in a Go project.
- The _______ statement in Go is used to iterate over elements in an array, slice, string, or map.
- Can 'panic()' be called inside a deferred function in Go?
- You're working on a project with a team, and you want to enforce a convention for naming test functions. How would you achieve this in Go unit testing?