-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.go
54 lines (46 loc) · 858 Bytes
/
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
package main
import (
"fmt"
"math"
)
// shape interface
type shape interface {
area() float64
circumf() float64
}
type square struct {
length float64
}
type circle struct {
radius float64
}
// square methods
func (s square) area() float64 {
return s.length * s.length
}
func (s square) circumf() float64 {
return s.length * 4
}
// circle methods
func (c circle) area() float64 {
return math.Pi * c.radius * c.radius
}
func (c circle) circumf() float64 {
return 2 * math.Pi * c.radius
}
func printShapeInfo(s shape) {
fmt.Printf("area of %T is: %0.2f \n", s, s.area())
fmt.Printf("circumference of %T is: %0.2f \n", s, s.circumf())
}
func main() {
shapes := []shape{
square{length: 15.2},
circle{radius: 7.5},
circle{radius: 12.3},
square{length: 4.9},
}
for _, v := range shapes {
printShapeInfo(v)
fmt.Println("---")
}
}