-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare.go
85 lines (66 loc) · 1.36 KB
/
square.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
85
package main
import (
"fmt"
"log"
"math"
)
// Point ia a 2D point
type Point struct {
X float64
Y float64
}
func (p *Point) Move(dx float64, dy float64) {
p.X += dx
p.Y += dy
}
type Square struct {
Center Point
Length float64
}
func NewSquare(x float64, y float64, length float64) (*Square, error) {
if length <= 0 {
return nil, fmt.Errorf("Length must be > 0 (was %f)", length)
}
square := &Square{
Center: Point{X: x, Y: y},
Length: length,
}
return square, nil
}
func (s *Square) Move(dx float64, dy float64) {
s.Center.Move(dx, dy)
}
func (s *Square) Area() float64 {
return s.Length * s.Length
}
type Circle struct {
Radius float64
}
func (c *Circle) Area() float64 {
return math.Pi * c.Radius * c.Radius
}
func sumAreas(shapes []Shape) float64 {
total := 0.0
for _, shape := range shapes {
total += shape.Area()
}
return total
}
type Shape interface {
Area() float64
}
func main() {
s, err := NewSquare(3, 3, 4)
if err != nil {
//fmt.Printf("Error: can't create a square - %s\n", err)
//os.Exit(1)
log.Fatalf("Error: can't create a square - %s\n", err)
}
s.Move(2, 3)
fmt.Printf("Area of the square %+v is %f\n", s, s.Area())
c := &Circle{10}
fmt.Printf("Area of the circle %+v is %f\n", c, c.Area())
shapes := []Shape{s, c}
sa := sumAreas(shapes)
fmt.Printf("The sum area of the square and the circle is %f\n", sa)
}