-
Notifications
You must be signed in to change notification settings - Fork 40
/
data.go
98 lines (87 loc) · 1.58 KB
/
data.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
98
package main
import "strconv"
// Company represents a company
type Company struct {
ID string
Company string
Contact string
Country string
}
// List of companies
type Companies struct {
companies []Company
}
// Company data access
var data Companies
func init() {
data = Companies{
companies: []Company{
{
ID: "1",
Company: "Amazon",
Contact: "Jeff Bezos",
Country: "United States",
},
{
ID: "2",
Company: "Apple",
Contact: "Tim Cook",
Country: "United States",
},
{
ID: "3",
Company: "Microsoft",
Contact: "Satya Nadella",
Country: "United States",
},
},
}
}
func (c *Companies) getByID(id string) Company {
var result Company
for _, i := range c.companies {
if i.ID == id {
result = i
break
}
}
return result
}
func (c *Companies) update(company Company) {
result := []Company{}
for _, i := range c.companies {
if i.ID == company.ID {
i.Company = company.Company
i.Contact = company.Contact
i.Country = company.Country
}
result = append(result, i)
}
c.companies = result
}
func (c *Companies) add(company Company) {
max := 0
for _, i := range c.companies {
n, _ := strconv.Atoi(i.ID)
if n > max {
max = n
}
}
max++
id := strconv.Itoa(max)
c.companies = append(c.companies, Company{
ID: id,
Company: company.Company,
Contact: company.Contact,
Country: company.Country,
})
}
func (c *Companies) delete(id string) {
result := []Company{}
for _, i := range c.companies {
if i.ID != id {
result = append(result, i)
}
}
c.companies = result
}