-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinterfaces.go
84 lines (65 loc) · 1.48 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package workshop
import (
"math"
)
// Shape interface has an area, circumference, and volume methods
type Shape interface {
area()
circumference()
volume()
}
// Circle is a type of shape that has a radius and uses the value PI
type Circle struct {
radius float64
PI float64
}
func (circle *Circle) area() float64 {
return circle.PI * math.Pow(circle.radius, 2)
}
func (circle *Circle) circumference() float64 {
return 2 * circle.PI * circle.radius
}
// Square is a shape that has 4 equal sides
type Square struct {
side float64
}
func (square *Square) area() float64 {
return math.Pow(square.side, 2)
}
type Sphere struct {
PI float64
radius float64
}
func (sphere *Sphere) volume() float64 {
return (4 / 3) * sphere.PI * math.Pow(sphere.radius, 3)
}
// Cube is a 3-dimensional circle
type Cube struct {
side float64
}
func (cube *Cube) volume() float64 {
return math.Pow(cube.side, 3)
}
func (square *Square) volume() float64 {
return math.Pow(square.side, 3)
}
// Rectangle is a 4 sided polygon
type Rectangle struct {
height int
width int
}
func (rectangle *Rectangle) area() int {
return rectangle.height * rectangle.width
}
func interfaces() {
circle := Circle{radius: 5.5, PI: math.Pi}
areaOfCircle := circle.area()
assert(areaOfCircle == 55)
assert(circle.circumference() == 88)
var square = Square{5}
assert(square.area() == 36)
cube := new(Cube)
assert(cube.volume() == 8)
var rectangle = Rectangle{width: 4, height: 5}
assert(rectangle.area() == 2)
}