Project Shape interface
Context
Section titled “Context”Build a program that defines a Shape interface with methods Area() and Perimeter(). Implement concrete types Circle, Rectangle, and Triangle. Then write a function that prints the details of any shape.
This project demonstrates:
- Interface declaration and implementation
- Polymorphism via interfaces
- Method definitions on structs
Step‑by‑step code
Section titled “Step‑by‑step code”package main
import ( "fmt" "math")
// Shape interfacetype Shape interface { Area() float64 Perimeter() float64}
// Circletype Circle struct { Radius float64}
func (c Circle) Area() float64 { return math.Pi * c.Radius * c.Radius}
func (c Circle) Perimeter() float64 { return 2 * math.Pi * c.Radius}
// Rectangletype Rectangle struct { Width, Height float64}
func (r Rectangle) Area() float64 { return r.Width * r.Height}
func (r Rectangle) Perimeter() float64 { return 2 * (r.Width + r.Height)}
// Triangle (equilateral)type Triangle struct { Side float64}
func (t Triangle) Area() float64 { return (math.Sqrt(3) / 4) * t.Side * t.Side}
func (t Triangle) Perimeter() float64 { return 3 * t.Side}
// Function that works with any Shapefunc printShapeInfo(s Shape) { fmt.Printf("Area: %.2f, Perimeter: %.2f\n", s.Area(), s.Perimeter())}
func main() { shapes := []Shape{ Circle{Radius: 5}, Rectangle{Width: 4, Height: 7}, Triangle{Side: 3}, }
for _, s := range shapes { printShapeInfo(s) }}Output
Section titled “Output”Area: 78.54, Perimeter: 31.42Area: 28.00, Perimeter: 22.00Area: 3.90, Perimeter: 9.00