This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path07-map.slide
More file actions
139 lines (115 loc) · 3.3 KB
/
07-map.slide
File metadata and controls
139 lines (115 loc) · 3.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Maps in Go
22 Feb 2017
Tags: edmontongo, golang, workshop
Lukasz Rozycki
[[https://lukaszr.com]]
* Maps
- Hash Table, Dictionary
- Mapping from key to a value
- Fast lookups, adds, and deletes
- A Go map type looks like this:
map[KeyType]ValueType
- Maps are not safe for concurrent use
* What can be a key?
- Any type that is comparable
== equal
!= not equal
- boolean, numeric, string, pointer
- interface type
- struct or array containing above
- Not slice, map, nor function
* Creating a map
- composite literal (note trailing comma)
.play -edit 07-map/compositeliteral.go /START/,/END/
- empty map
.play -edit 07-map/emptyliteral.go /START/,/END/
* Lookup
- index expression
.play -edit 07-map/lookup.go /START/,/END/
- zero value
.play -edit 07-map/zerovalue.go /START/,/END/
- what about Baby?
.play -edit 07-map/commaok1.go /START/,/END/
* Lookup
- "comma ok" idiom
.play -edit 07-map/commaok2.go /START/,/END/
- concise notation
.play -edit 07-map/commaok3.go /START/,/END/
* Lookup
- ignore age
.play -edit 07-map/commaok4.go /START/,/END/
- why underscore?
.play -edit 07-map/commaok5.go /START/,/END/
* Addition
- index expression
.play -edit 07-map/put.go /START/,/END/
- read and modify
.play -edit 07-map/put2.go /START/,/END/
* Deletion
- delete built-in function
func delete(m map[Type]Type1, key Type)
.play -edit 07-map/delete.go /START/,/END/
- delete no-op
.play -edit 07-map/delete2.go /START/,/END/
* Wrong types
- get wrong type
.play -edit 07-map/lookup3.go /START/,/END/
- add wrong type
.play -edit 07-map/put3.go /START/,/END/
- delete wrong type
.play -edit 07-map/delete3.go /START/,/END/
* Set with map
- create
set := map[int]bool{}
- add to a aset
set[3] = true
- remove from set
delete(set, 7)
- check if set contains
if(set[13]) ...
* Iteration
- for range
.play -edit 07-map/for.go /START/,/END/
- order is random
.play -edit 07-map/for2.go /START/,/END/
* Iteration
- key and value
.play -edit 07-map/for4.go /START/,/END/
- blank key
.play -edit 07-map/for5.go /START/,/END/
* Map is a reference type
- zero value
.play -edit 07-map/nil.go /START/,/END/
- add
.play -edit 07-map/nil2.go /START/,/END/
- retrieve
.play -edit 07-map/nil3.go /START/,/END/
- delete (no-op)
.play -edit 07-map/nil4.go /START/,/END/
* Map is a reference type
- passing to a function
.code 07-map/ref.go /FUNC_S/,/FUNC_E/
.play -edit 07-map/ref.go /START/,/END/
- assigning to a second variable
.play -edit 07-map/ref2.go /START/,/END/
* Exercises - bank
func deposit(bank map[string]int32, name string, amount int32) bool
// if not a client, than assume initial deposit
// if balance would overflow return false
// amount cannot be negative
func withdraw(bank map[string]int32, name string, amount int32, maxOverdraft int32) bool
// final balance cannot be less than max_overdraft
// only clients can withdraw
// amount cannot be negative
*
* Solutions
- Exercise deposit
.code 07-map/ex2.go /DEPOSIT_S/,/DEPOSIT_E/
* Solutions
- Exercise withdraw
.code 07-map/ex2.go /WITHDRAW_S/,/WITHDRAW_E/
* Resources
- [[https://blog.golang.org/go-maps-in-action][Go maps in action]]
- [[https://golang.org/doc/effective_go.html#maps][Effective Go]] Maps
- [[https://tour.golang.org/moretypes/19][A Tour of Go]] Maps
- [[https://www.manning.com/books/learn-go][Learn Go]] by Nathan Youngman Ch. 16. The ever versatile map