|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | +) |
| 7 | + |
| 8 | +// Go Interface - `Shape` |
| 9 | +type Shape interface { |
| 10 | + Area() float64 |
| 11 | + Perimeter() float64 |
| 12 | +} |
| 13 | + |
| 14 | +// ================================================================== |
| 15 | +// Struct type `Circle` - implements the `Shape` interface implicitly |
| 16 | +type Circle struct { |
| 17 | + Radius float64 |
| 18 | +} |
| 19 | + |
| 20 | +func (c Circle) Area() float64 { |
| 21 | + return math.Pi * c.Radius * c.Radius |
| 22 | +} |
| 23 | + |
| 24 | +func (c Circle) Perimeter() float64 { |
| 25 | + return 2 * math.Pi * c.Radius |
| 26 | +} |
| 27 | + |
| 28 | +func (c Circle) Diameter() float64 { |
| 29 | + return 2 * c.Radius |
| 30 | +} |
| 31 | + |
| 32 | +// ==================================================================== |
| 33 | +// Struct type `Rectangle` - implements the `Shape` interface implicitly |
| 34 | +type Rectangle struct { |
| 35 | + Length, Width float64 |
| 36 | +} |
| 37 | + |
| 38 | +func (r Rectangle) Area() float64 { |
| 39 | + return r.Length * r.Width |
| 40 | +} |
| 41 | + |
| 42 | +func (r Rectangle) Perimeter() float64 { |
| 43 | + return 2 * (r.Length + r.Width) |
| 44 | +} |
| 45 | + |
| 46 | +// ================================================================================== |
| 47 | +// Generic function to calculate the total area of multiple shapes of different types |
| 48 | +func CalculateTotalArea(shapes ...Shape) float64 { |
| 49 | + totalArea := 0.0 |
| 50 | + for _, s := range shapes { |
| 51 | + totalArea += s.Area() |
| 52 | + } |
| 53 | + return totalArea |
| 54 | +} |
| 55 | + |
| 56 | +// ==================================================================== |
| 57 | +// Interfaces can also be used as fields |
| 58 | +type MyDrawing struct { |
| 59 | + shapes []Shape |
| 60 | + bgColor string |
| 61 | + fgColor string |
| 62 | +} |
| 63 | + |
| 64 | +func (drawing MyDrawing) Area() float64 { |
| 65 | + totalArea := 0.0 |
| 66 | + for _, s := range drawing.shapes { |
| 67 | + totalArea += s.Area() |
| 68 | + } |
| 69 | + return totalArea |
| 70 | +} |
| 71 | + |
| 72 | +// ================================================================== |
| 73 | +func main() { |
| 74 | + var s Shape = Circle{5.0} |
| 75 | + fmt.Printf("Shape Type = %T, Shape Value = %v\n", s, s) |
| 76 | + fmt.Printf("Area = %f, Perimeter = %f\n\n", s.Area(), s.Perimeter()) |
| 77 | + |
| 78 | + var s1 Shape = Rectangle{4.0, 6.0} |
| 79 | + fmt.Printf("Shape Type = %T, Shape Value = %v\n", s1, s1) |
| 80 | + fmt.Printf("Area = %f, Perimeter = %f\n", s1.Area(), s1.Perimeter()) |
| 81 | + |
| 82 | + totalArea := CalculateTotalArea(Circle{2}, Rectangle{4, 5}, Circle{10}) |
| 83 | + fmt.Println("Total area = ", totalArea) |
| 84 | + |
| 85 | + drawing := MyDrawing{ |
| 86 | + shapes: []Shape{ |
| 87 | + Circle{2}, |
| 88 | + Rectangle{3, 5}, |
| 89 | + Rectangle{4, 7}, |
| 90 | + }, |
| 91 | + bgColor: "red", |
| 92 | + fgColor: "white", |
| 93 | + } |
| 94 | + |
| 95 | + fmt.Println("Drawing", drawing) |
| 96 | + fmt.Println("Drawing Area = ", drawing.Area()) |
| 97 | + |
| 98 | + s = Circle{5} |
| 99 | + fmt.Printf("(%v, %T)\n", s, s) |
| 100 | + fmt.Printf("Shape area = %v\n", s.Area()) |
| 101 | + |
| 102 | + s = Rectangle{4, 7} |
| 103 | + fmt.Printf("(%v, %T)\n", s, s) |
| 104 | + fmt.Printf("Shape area = %v\n", s.Area()) |
| 105 | + |
| 106 | +} |
0 commit comments