-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (78 loc) · 1.54 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"errors"
"fmt"
"github.com/kelvinator07/design-patterns-in-go/mystrings"
)
type Copier interface {
Copy(sourceFile string, destinationFile string) (bytesCopied int)
}
type car struct {
Make string
Model string
Height int
Width int
// Wheel is a field containing an anonymous struct
Wheel struct {
Radius int
Material string
}
}
func divide(dividend, divisor int) (int, error) {
if divisor == 0 {
return 0, errors.New("Can't divide by zero")
}
return dividend / divisor, nil
}
type car2 struct {
make string
model string
}
type truck struct {
// "car" is embedded, so the definition of a
// "truck" now also additionally contains all
// of the fields of the car struct
car2
bedSize int
}
type shape interface {
area() float64
}
type circle struct {
radius float64
}
// c, ok := s.(circle)
// if !ok {
// // log an error if s isn't a circle
// log.Fatal("s is not a circle")
// }
// radius := c.radius
func main() {
fmt.Println(mystrings.Reverse("hello world"))
result, error := divide(0, 0)
if error != nil {
fmt.Println(error)
}
fmt.Println("End", result)
myCar := struct {
Make string
Model string
}{
"tesla",
"model 3",
}
fmt.Println("Car Make", myCar.Make)
fmt.Println("Car Model", myCar.Model)
lanesTruck := truck{
bedSize: 10,
car2: car2{
make: "toyota",
model: "camry",
},
}
fmt.Println(lanesTruck.bedSize)
// embedded fields promoted to the top-level
// instead of lanesTruck.car.make
fmt.Println(lanesTruck.make)
fmt.Println(lanesTruck.model)
}