-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
62 lines (50 loc) · 898 Bytes
/
map.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
package main
import (
"fmt"
)
type user struct {
name, email string
}
func (u *user) ChangeEmail(email string) {
u.email = email
}
func (u user) String() string {
return fmt.Sprintf("%s (%s)", u.name, u.email)
}
type userGroup struct {
users map[int]*user
}
func (ug userGroup) String() string {
output := "["
for key, val := range ug.users {
output += fmt.Sprintf("%d: {%s}; ", key, val)
}
output += "]"
return output
}
// main magic goes here
func (ug *userGroup) mapOverUsers(fn func(u *user)) {
for _, user := range ug.users {
fn(user)
}
}
func main() {
ug := userGroup{
map[int]*user{
0: &user{
name: "Max",
email: "[email protected]"},
1: &user{
name: "Nati",
email: "[email protected]"},
2: &user{
name: "Alex",
email: "[email protected]"},
},
}
fmt.Println(ug)
ug.mapOverUsers(func(u *user) {
u.ChangeEmail("new email")
})
fmt.Println(ug)
}