Go Interface Design: Shape Area Calculator
This challenge focuses on defining and implementing interfaces in Go to create a flexible system for calculating the area of different geometric shapes. Understanding interfaces is crucial for writing idiomatic and maintainable Go code, enabling polymorphism and decoupling.
Problem Description
Your task is to design and implement an interface in Go that represents a generic "Shape." This interface should define a method to calculate the area of the shape. You will then create concrete types for at least two different shapes (e.g., Circle, Rectangle) that implement this interface.
Requirements:
- Define an Interface: Create a Go interface named
Shapethat has a single method:Area() float64. This method should return the calculated area of the shape as afloat64. - Implement Concrete Types:
- Create a
Circlestruct with aRadiusfield (float64). Implement theArea()method forCircleto calculate its area using the formula $\pi * radius^2$. - Create a
Rectanglestruct withWidthandHeightfields (both float64). Implement theArea()method forRectangleto calculate its area using the formulawidth * height.
- Create a
- Demonstrate Polymorphism: Write a function that accepts a slice of
Shapeinterfaces and iterates through it, calculating and printing the area of each shape.
Expected Behavior:
When your code is executed, it should be able to:
- Instantiate
CircleandRectangleobjects. - Call the
Area()method on these objects, and each should return the correct area. - Process a collection of mixed
CircleandRectangleobjects using theShapeinterface, demonstrating that the correctArea()method is invoked for each specific type.
Edge Cases to Consider:
- Shapes with zero dimensions (e.g., radius = 0, width = 0, height = 0). The area should be 0.
Examples
Example 1:
Input:
- A Circle with Radius = 5.0
- A Rectangle with Width = 4.0, Height = 6.0
Output:
Circle Area: 78.53981633974483
Rectangle Area: 24.0
Explanation: The Area() method is called on the Circle and Rectangle instances, returning their respective calculated areas.
Example 2:
Input:
- A Circle with Radius = 0.0
- A Rectangle with Width = 10.0, Height = 0.0
Output:
Circle Area: 0.0
Rectangle Area: 0.0
Explanation: Shapes with zero dimensions correctly result in an area of 0.0.
Example 3: Demonstrating Polymorphic Processing
Input:
Shapes = [
Circle{Radius: 3.0},
Rectangle{Width: 2.0, Height: 5.0},
Circle{Radius: 1.0},
]
Output:
Processing shapes:
Shape 1 Area: 28.274333882308138
Shape 2 Area: 10.0
Shape 3 Area: 3.141592653589793
Explanation: A function iterating over a slice of Shape interface correctly invokes the Area() method for each concrete shape type.
Constraints
- All dimensions (Radius, Width, Height) will be non-negative
float64values. - The
math.Piconstant from themathpackage should be used for circle calculations. - Your solution should be written entirely in Go.
Notes
- Consider how you will use the
Shapeinterface in a function to process a collection of different shapes without needing to know their specific underlying types. This is the core concept of polymorphism in Go. - Think about how you would add a new shape (e.g., a
Triangle) to this system in the future. How would the interface design facilitate this extensibility?