-
Notifications
You must be signed in to change notification settings - Fork 7
/
presentation.slide
354 lines (184 loc) · 8.86 KB
/
presentation.slide
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
MongoDB with Go
Cory LaNou, InfluxDB
cory@influxdb.com
@corylanou
* What is MongoDB
MongoDB (from "humongous") is an open-source document database, and the leading NoSQL database. Written in C++.
.link http://www.mongodb.org/ MongoDB
* What is NoSQL
Trivia bonus question: What does "NoSQL" stands for?
* NoSQL
It does NOT mean "no sql"
It means "Not only SQL"
* Setting up MongoDB
Since installing MongoDB goes beyond the scope of this presentation,
we will use mongolab.
.link https://mongolab.com/ mongolab home page
.link https://mongolab.com/signup/ 1) Sign up
.link https://mongolab.com/create 2) Create instance
3) Select "Single-Node", then "Sandbox". This is the free option.
4) Give it the database name "beginning_go".
5) Click "Create New Mongo Deployment"
* mongolab instance url and password
To find the url and password, click the name of the newly created instance.
In this case "beginning_go"
* Add a user
.link https://mongolab.com/databases/beginning_go#users Add user
username: go
password: I will tell you this at presentation time
* Connecting
If you have the mongo tools installed locally, you can connect to your instance like this:
mongo <url>/beginning_go -u <dbuser> -p <dbpassword>
This is not necessary for this workshop
* mgo
mgo (pronounced as mango) is a MongoDB driver for the Go language that implements a rich and well tested selection of features under a very simple API following standard Go idioms.
To get the library locally, run this command at the command line:
go get gopkg.in/mgo.v2
.link http://godoc.org/gopkg.in/mgo.v2 API Documentation
* spew
This is a go library we will use to "pretty print" from our examples.
go get github.com/davecgh/go-spew/spew
.link https://github.com/davecgh/go-spew API Documentation
* Setting up our environment
We will need some environment variables to make our life easier.
*Mac/Linux*
export mongo_url=<your url>
export mongo_user=go
export mongo_password=<your password>
export mongo_collection=to_dos
Test this by running:
printenv | grep mongo
*Windows*
set mongo_url=<your url>
set mongo_user=go
set mongo_password=<your password>
set mongo_collection=to_dos
* Environment Variables - Advice
I create a local _dev.env_ file that I source and add it to my *.gitignore* file so it doesn't check in.
This allows me to quickly open the project, run:
source dev.env
and have all necessary environment variables
for my local project set up.
Summary:
1) Create dev.env file.
2) Add necessary environment variables
3) Add "dev.env" to your ".gitignore" file if using git
4) run:
source dev.env
* Creating a session - Imports
Define the package and imports we will use:
.code 01_session/main.go /START IMPORT OMIT/,/END IMPORT OMIT/
* Creating a session - Main block
Open the "main" block
.code 01_session/main.go /START MAIN OMIT/,/END MAIN OMIT/
* Creating a session - Getting environment variables
Get our environment variables
.code 01_session/main.go /START ENV OMIT/,/END ENV OMIT/
.link http://golang.org/pkg/os/ os package documenation
* Creating a session - Declare some local variables
Decalre some local variables
.code 01_session/main.go /START VAR OMIT/,/END VAR OMIT/
* Creating a session - Dialing up the server
Dial up the server
.code 01_session/main.go /START DIAL OMIT/,/END DIAL OMIT/
.link http://golang.org/pkg/fmt/ fmt package documenation
* Creating a session - Getting the collection
Get our collection
.play 01_session/main.go /START FINAL OMIT/,/END FINAL OMIT/
* Creating a session - Running the example
To run this:
go run main.go
You should get something like this returned:
Collection: &{Database:0xc20801ed40 Name:to_dos FullName:beginning_go.to_dos}
Here is the entire file:
.link https://github.com/corylanou/go-mongo-presentation/blob/master/01_session/main.go main.go
* Structs and Tags - Imports
.code 02_structs/main.go /START IMPORT OMIT/,/END IMPORT OMIT/
* Structs and Tags - Type and struct tags
.code 02_structs/main.go /START TYPE OMIT/,/END TYPE OMIT/
.link http://golang.org/pkg/reflect/#StructTag API documentation on StructTag
*Note:* Getting struct tags wrong can make your life miserable. Be sure to always run go vet on your code:
go vet ./...
You may need to install go vet first:
go get -u golang.org/x/tools/cmd/vet
* Structs and Tags - creating a todo
.play 02_structs/main.go /START MAIN OMIT/,/END MAIN OMIT/
* Structs and Tags - Running the example
To run this:
go run main.go
You should get something like this returned:
(main.Todo) {
Task: (string) (len=8) "Demo mgo",
Created: (time.Time) 2015-01-10 11:06:39.876506859 -0700 MST,
Updated: (time.Time) 0001-01-01 00:00:00 +0000 UTC,
Completed: (time.Time) 0001-01-01 00:00:00 +0000 UTC
}
Here is the entire file:
.link https://github.com/corylanou/go-mongo-presentation/blob/master/02_structs/main.go main.go
*NOTE:* Zero Time can be detected
.link http://golang.org/pkg/time/#Time.IsZero Time.IsZero
* Upserting data - Adding BSON import
.code 03_upsert/main.go /START IMPORT OMIT/,/END IMPORT OMIT/
.link http://godoc.org/gopkg.in/mgo.v2/bson mgo bson API documentation
* Upserting data - Adding ID to the Todo type
.code 03_upsert/main.go /START TYPE OMIT/,/END TYPE OMIT/
*NOTE:* There are some tricks you will want to do here for ID if you are going to serialize to JSON. We'll cover this at the end of the presentation.
* Upserting data - Upsert the data
.play 03_upsert/main.go /START FINAL OMIT/,/END FINAL OMIT/
Here is the entire file:
.link https://github.com/corylanou/go-mongo-presentation/blob/master/03_upsert/main.go main.go
* Reading/Writing via Find/Apply
.play 04_read_write/main.go /START CHANGE OMIT/,/END CHANGE OMIT/
Here is the entire file:
.link https://github.com/corylanou/go-mongo-presentation/blob/master/04_read_write/main.go main.go
* Aggregation - Results Type Definition
.code 05_aggregation/main.go /START TYPE OMIT/,/END TYPE OMIT/
* Aggregation - Seeding data
.code 05_aggregation/main.go /START SEED OMIT/,/END SEED OMIT/
* Aggregation - Pipeline
.code 05_aggregation/main.go /START PIPELINE OMIT/,/END PIPELINE OMIT/
* Aggregation - Results
.code 05_aggregation/main.go /START RESULTS OMIT/,/END RESULTS OMIT/
* Aggregation - Iter
.play 05_aggregation/main.go /START ITER OMIT/,/END ITER OMIT/
*NOTE:* You will have to remove your existing data before running this as we changed the structure.
* ID fields and the downfalls of BSON
If you serialize this struct to JSON and then back from json to this struct, the world will blow up.
To avoid this, we create a type for the ID as a string and add some bson magic.
.code 06_ids/main.go /START TYPE OMIT/,/END TYPE OMIT/
* ID - As a type
.code 06_ids/main.go /START TYPE_ID OMIT/,/END TYPE_ID OMIT/
This also has the side benefit of never allowing me to specify another ID as a TODO ID.
Example, I have a placeID and a userID, even though they are both strings, now with a custom type I can never assign a userID to a placeID in the struct.
* ID - Boilerplate
.code 06_ids/main.go /START BOILERPLATE OMIT/,/END BOILERPLATE OMIT/
*NOTE:* In go 1.4 this would be a great use of the generate tool
* ID - BSON Marshalling
.code 06_ids/main.go /START BSON OMIT/,/END BSON OMIT/
.link http://godoc.org/gopkg.in/mgo.v2/bson#Getter BSON Getter
.link http://godoc.org/gopkg.in/mgo.v2/bson#Setter BSON Setter
* Stubbing and testing calls to the database
Stubbing out our calls allows us to create quick unit tests instead of actually calling into the database.
* The interface
Declaring an interface allows you to create a stub in your tests later
.code 07_stubbing_and_testing/handler.go /START TYPE OMIT/,/END TYPE OMIT/
* The handler PART 1
There are many ways to attach context to your handlers, this is a simple way but there are better ways.
.code 07_stubbing_and_testing/handler.go /START HANDLER1 OMIT/,/END HANDLER1 OMIT/
* The handler PART 2
.code 07_stubbing_and_testing/handler.go /START HANDLER2 OMIT/,/END HANDLER2 OMIT/
Here is the entire file:
.link https://github.com/corylanou/go-mongo-presentation/blob/master/07_stubbing_and_testing/handler.go handler.go
* The stub
.code 07_stubbing_and_testing/handler_test.go /START STUB OMIT/,/END STUB OMIT/
* Testing Found - Setting up the stub
.code 07_stubbing_and_testing/handler_test.go /START FOUND1 OMIT/,/END FOUND1 OMIT/
* Testing Found - Setting up the http test
.code 07_stubbing_and_testing/handler_test.go /START FOUND2 OMIT/,/END FOUND2 OMIT/
.link http://golang.org/pkg/net/http/httptest/#pkg-examples httptest
* Testing Not Found
.code 07_stubbing_and_testing/handler_test.go /START NOTFOUND1 OMIT/,/END NOTFOUND1 OMIT/
.code 07_stubbing_and_testing/handler_test.go /START NOTFOUND2 OMIT/,/END NOTFOUND2 OMIT/
* Testing Internal Server Error
.code 07_stubbing_and_testing/handler_test.go /START SERVERERROR1 OMIT/,/END SERVERERROR1 OMIT/
.code 07_stubbing_and_testing/handler_test.go /START SERVERERROR2 OMIT/,/END SERVERERROR2 OMIT/