forked from shijuvar/go-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
composition1.go
80 lines (71 loc) · 1.49 KB
/
composition1.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
// Example program for Composition of types
package main
import (
"fmt"
"time"
)
type Person struct {
FirstName, LastName string
Dob time.Time
Email, Location string
}
//A person method
func (p Person) PrintName() {
fmt.Printf("\n%s %s\n", p.FirstName, p.LastName)
}
//A person method
func (p Person) PrintDetails() {
fmt.Printf("[Date of Birth: %s, Email: %s, Location: %s ]\n", p.Dob.String(), p.Email, p.Location)
}
type Admin struct {
Person //type embedding for composition
Roles []string
}
//overrides PrintDetails
func (a Admin) PrintDetails() {
//Call person PrintDetails
a.Person.PrintDetails()
fmt.Println("Admin Roles:")
for _, v := range a.Roles {
fmt.Println(v)
}
}
type Member struct {
Person //type embedding for composition
Skills []string
}
//overrides PrintDetails
func (m Member) PrintDetails() {
//Call person PrintDetails
m.Person.PrintDetails()
fmt.Println("Skills:")
for _, v := range m.Skills {
fmt.Println(v)
}
}
func main() {
alex := Admin{
Person{
"Alex",
"John",
time.Date(1970, time.January, 10, 0, 0, 0, 0, time.UTC),
"alex@email.com",
"New York"},
[]string{"Manage Team", "Manage Tasks"},
}
shiju := Member{
Person{
"Shiju",
"Varghese",
time.Date(1979, time.February, 17, 0, 0, 0, 0, time.UTC),
"shiju@email.com",
"Kochi"},
[]string{"Go", "Docker", "Kubernetes"},
}
//call methods for alex
alex.PrintName()
alex.PrintDetails()
//call methods for shiju
shiju.PrintName()
shiju.PrintDetails()
}