Hone logo
Hone
Problems

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:

  1. Define an Interface: Create a Go interface named Shape that has a single method: Area() float64. This method should return the calculated area of the shape as a float64.
  2. Implement Concrete Types:
    • Create a Circle struct with a Radius field (float64). Implement the Area() method for Circle to calculate its area using the formula $\pi * radius^2$.
    • Create a Rectangle struct with Width and Height fields (both float64). Implement the Area() method for Rectangle to calculate its area using the formula width * height.
  3. Demonstrate Polymorphism: Write a function that accepts a slice of Shape interfaces 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 Circle and Rectangle objects.
  • Call the Area() method on these objects, and each should return the correct area.
  • Process a collection of mixed Circle and Rectangle objects using the Shape interface, demonstrating that the correct Area() 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 float64 values.
  • The math.Pi constant from the math package should be used for circle calculations.
  • Your solution should be written entirely in Go.

Notes

  • Consider how you will use the Shape interface 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?
Loading editor...
go