-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaces.go
64 lines (51 loc) · 1.45 KB
/
interfaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"math"
)
// Go Interface - `Shape`
type Shape interface {
Area() float64
Perimeter() float64
}
// Struct type `Rectangle` - implements the `Shape` interface by implementing all its methods.
type Rectangle struct {
Length, Width float64
}
func (r Rectangle) Area() float64 {
return r.Length * r.Width
}
func (r Rectangle) Perimeter() float64 {
return 2 * (r.Length + r.Width)
}
// Struct type `Circle` - implements the `Shape` interface by implementing all its methods.
type 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
}
func (c Circle) Diameter() float64 {
return 2 * c.Radius
}
// Generic function to calculate the total area of multiple shapes of different types
func CalculateTotalArea(shapes ...Shape) float64 {
totalArea := 0.0
for _, s := range shapes {
totalArea += s.Area()
}
return totalArea
}
func main() {
var s Shape = Circle{5.0}
fmt.Printf("Shape Type = %T, Shape Value = %v\n", s, s)
fmt.Printf("Area = %f, Perimeter = %f\n\n", s.Area(), s.Perimeter())
s = Rectangle{4.0, 6.0}
fmt.Printf("Shape Type = %T, Shape Value = %v\n", s, s)
fmt.Printf("Area = %f, Perimeter = %f\n", s.Area(), s.Perimeter())
totalArea := CalculateTotalArea(Circle{2}, Rectangle{4, 5}, Circle{10})
fmt.Println("Total area = ", totalArea)
}