-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupingTypes.go
74 lines (62 loc) · 1.64 KB
/
groupingTypes.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
package codesnippets
//Grouping Types
import "fmt"
// Speaker speak[ER] provides a common behavior for all concrete types
// to follow if they want to be part of this group. This
// is a contract for these concrete types to follow.
type Speaker interface {
Speak()
}
// Dog contains everything a Dog needs
type Dog struct {
Name string
IsMammal bool
PackFactor int
}
// Speak knows how to speak like a dog.
// This makes a Dog now part of a group of concrete
// type that know how to speak.
func (d Dog) Speak() {
fmt.Println("Woof!",
"My name is,", d.Name,
", it is", d.IsMammal,
"I am a mammal with a pack factor of ", d.PackFactor)
}
// Cat contains everything a Cat needs
type Cat struct {
Name string
IsMammal bool
ClimbFactor int
}
func (c Cat) Speak() {
fmt.Println("Meow!",
"My name is,", c.Name,
", it is", c.IsMammal,
"I am a mammal with a climb factor of ", c.ClimbFactor)
}
func Callmain() {
fmt.Println("Animal Example:")
// create a list of Animals that know how to speak
speakers := []Speaker{
Dog{
Name: "Fido",
IsMammal: true,
PackFactor: 5,
},
Cat{
Name: "Whiskers",
IsMammal: true,
ClimbFactor: 5,
},
}
// Have the Animals speak
for _, spkr := range speakers {
spkr.Speak()
}
}
//Here are some guidelines around declaring types:
// - Declare types that represent something new or unique
// - Validate that a value of any type is created or used on its own
// - Embed types to reuse existing BEHAVIORS you need to satisfy
// - Question types that are an alias or abstraction for an existing type
// - Question types whose sole purpose is to share common state