-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.go
More file actions
82 lines (73 loc) · 1.3 KB
/
inheritance.go
File metadata and controls
82 lines (73 loc) · 1.3 KB
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
package main
import "fmt"
//Inheritance in Go is not like what you find in C#, C++, etc
//Embedding structs allows inheriting properties
type animal struct {
name string
legs int
fur bool
feathers bool
sound string
}
type dog struct {
animal
leash string
}
type cat struct {
animal
leash string
hairballs bool
}
type bird struct {
animal
wings int
}
// This is a bird, which is an animal
type birdOfPrey struct {
bird
talons string
}
var fido dog
var felix cat
var eagle birdOfPrey
func inheritance() {
generic := animal{name: "animal", legs: 0, fur: false, feathers: false}
fmt.Printf("basic animal: %+v\n", generic)
fido = dog{
animal: animal{
name: "dog",
legs: 4,
fur: true,
feathers: false,
sound: "bark",
},
leash: "yaaay lets go outside",
}
fmt.Printf("fido: %+v\n", fido)
felix = cat{
animal: animal{
name: "cat",
legs: 4,
fur: true,
feathers: false,
sound: "meow",
},
leash: "you will pay for this",
hairballs: true,
}
fmt.Printf("felix: %+v\n", felix)
eagle = birdOfPrey{
bird: bird{
animal: animal{
name: "eagle",
legs: 2,
fur: false,
feathers: true,
sound: "screech",
},
wings: 2,
},
talons: "sharp",
}
fmt.Printf("eagle: %+v\n", eagle)
}