package main import "fmt" import "math" type Rectangle struct { x1, y1, x2, y2 float64 } func (r *Rectangle) length() float64 { return distance(r.x1, r.y1, r.x1, r.y2) } func (r *Rectangle) width() float64 { return distance(r.x1, r.y1, r.x2, r.y1) } func (r *Rectangle) area() float64 { return r.length() * r.width() } func (r *Rectangle) perimeter() float64 { return 2*r.length() + 2*r.width() } type Circle struct { x float64 y float64 r float64 } func (c *Circle) area() float64 { return math.Pi * c.r * c.r } func (c *Circle) perimeter() float64 { return 2 * math.Pi * c.r } type Shape interface { area() float64 perimeter() float64 } type MultiShape struct { shapes []Shape } func (m *MultiShape) area() float64 { var area float64 for _, s := range m.shapes { area += s.area() } return area } func (m *MultiShape) perimeter() float64 { var perimeter float64 for _, s := range m.shapes { perimeter += s.perimeter() } return perimeter } func distance(x1, y1, x2, y2 float64) float64 { a := x2 - x1 b := y2 - y1 return math.Sqrt(a*a + b*b) } func main() { c := &Circle{x: 1, y: 1, r: 4} r := &Rectangle{x2: 4, y2: 10} ms := MultiShape{[]Shape{c, r}} fmt.Println("Area", ms.area()) fmt.Println("Perimeter", ms.perimeter()) }