-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
69 lines (53 loc) · 1.06 KB
/
interface.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
65
66
67
68
69
package main
import (
"fmt"
"math"
)
type shape interface {
area() float64
perimeter() float64
}
type rectangle struct {
width, height float64
}
type circle struct {
radius float64
}
func (c circle) area() float64 {
return math.Pi * math.Pow(c.radius, 2)
}
func (c circle) perimeter() float64 {
return 2 * math.Pi * c.radius
}
func (r rectangle) area() float64 {
return r.height * r.width
}
func printCircle(c circle) {
fmt.Println("Shape:", c)
fmt.Println("Area:", c.area())
fmt.Println("Perimeter:", c.perimeter())
}
func printRectangle(r rectangle) {
fmt.Println("Shape:", r)
fmt.Println("Area:", r.area())
fmt.Println("Perimeter:", r.perimeter())
}
func print(s shape) {
fmt.Printf("Shape: %#v\n", s)
fmt.Printf("Area: %v\n", s.area())
fmt.Printf("Perimeter: %v\n", s.perimeter())
}
func (r rectangle) perimeter() float64 {
return 2 * (r.width + r.height)
}
func main() {
c1 := circle{radius: 5.}
r1 := rectangle{width: 3., height: 3.1}
printCircle(c1)
fmt.Println()
printRectangle(r1)
fmt.Println()
print(c1)
fmt.Println()
print(r1)
}