Designing and Implementing a Shape Interface in Go
This challenge focuses on understanding and implementing interfaces in Go. Interfaces are a powerful tool for achieving polymorphism and abstraction, allowing you to write code that can work with different types as long as they satisfy a common contract. You'll design and implement a Shape interface and then create concrete types that implement it.
Problem Description
You are tasked with designing and implementing a Shape interface in Go. The interface should define a method Area() that calculates and returns the area of a shape. You need to create three concrete types: Rectangle, Circle, and Triangle, each implementing the Shape interface. Each type should have its own specific data fields (width/height for Rectangle, radius for Circle, base/height for Triangle) and implement the Area() method accordingly. The goal is to demonstrate how interfaces enable you to treat different shape types uniformly through a common interface.
Key Requirements:
- Define a
Shapeinterface with anArea() float64method. - Create
Rectangle,Circle, andTrianglestructs. - Implement the
Area()method for each struct, conforming to theShapeinterface. - Write a function
PrintArea(shape Shape)that takes aShapeinterface as input and prints the area returned by itsArea()method. - Create instances of each shape type and pass them to the
PrintAreafunction to verify the implementation.
Expected Behavior:
The PrintArea function should correctly calculate and print the area of each shape type passed to it. The output should be formatted as "Area: [area value]".
Edge Cases to Consider:
- While not strictly required for this problem, consider how you might handle invalid input (e.g., negative dimensions) within the
Area()methods. For simplicity, you can assume valid positive inputs. - Think about how the interface allows you to easily add new shape types in the future without modifying the
PrintAreafunction.
Examples
Example 1:
Input: Rectangle{width: 4, height: 5}, Circle{radius: 3}, Triangle{base: 6, height: 8}
Output:
Area: 20
Area: 28.274333882308138
Area: 24
Explanation: The Area() method is called on each shape, calculating the area based on its dimensions. The PrintArea function then prints the returned area.
Example 2:
Input: Circle{radius: 1.5}
Output:
Area: 7.0685834705770345
Explanation: The Circle's Area() method calculates the area of the circle (πr²) and prints it.
Constraints
- All dimensions (width, height, radius, base) will be positive floating-point numbers.
- The
Area()method must return afloat64value. - The
PrintAreafunction should print the area to the console in the format "Area: [area value]". - No external libraries are allowed.
Notes
- Interfaces in Go are implicitly satisfied. A type implements an interface simply by having all the methods declared in the interface.
- Consider using
math.Pifor calculating the area of a circle. - This problem is designed to reinforce your understanding of interface definition, implementation, and usage. Think about the benefits of using interfaces in this scenario.