@@ -241,56 +241,6 @@ func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
241
241
}
242
242
```
243
243
244
- Now when we run the tests you get (or don't get - see above):
245
-
246
- ``` sh
247
- --- FAIL: TestCheckWebsites (0.00s)
248
- CheckWebsites_test.go:31: Wanted map[http://google.com:true http://blog.gypsydave5.com:true waat://furhurterwe.geds:false], got map[waat://furhurterwe.geds:false]
249
- FAIL
250
- exit status 1
251
- FAIL github.com/gypsydave5/learn-go-with-tests/concurrency/v1 0.010s
252
- ```
253
-
254
- This isn't great - why only one result? We might try and fix this by increasing
255
- the time we wait - try it if you like. It won't work. The problem here is that
256
- the variable ` url ` is reused for each iteration of the ` for ` loop - it takes
257
- a new value from ` urls ` each time. But each of our goroutines have a reference
258
- to the ` url ` variable - they don't have their own independent copy. So they're
259
- _ all_ writing the value that ` url ` has at the end of the iteration - the last
260
- url. Which is why the one result we have is the last url.
261
-
262
- To fix this:
263
-
264
- ``` go
265
- package concurrency
266
-
267
- import (
268
- " time"
269
- )
270
-
271
- type WebsiteChecker func (string ) bool
272
-
273
- func CheckWebsites (wc WebsiteChecker , urls []string ) map [string ]bool {
274
- results := make (map [string ]bool )
275
-
276
- for _ , url := range urls {
277
- go func (u string ) {
278
- results[u] = wc (u)
279
- }(url)
280
- }
281
-
282
- time.Sleep (2 * time.Second )
283
-
284
- return results
285
- }
286
- ```
287
-
288
- By giving each anonymous function a parameter for the url - ` u ` - and then
289
- calling the anonymous function with the ` url ` as the argument, we make sure that
290
- the value of ` u ` is fixed as the value of ` url ` for the iteration of the loop
291
- that we're launching the goroutine in. ` u ` is a copy of the value of ` url ` , and
292
- so can't be changed.
293
-
294
244
Now if you're lucky you'll get:
295
245
296
246
``` sh
@@ -413,9 +363,9 @@ func CheckWebsites(wc WebsiteChecker, urls []string) map[string]bool {
413
363
resultChannel := make (chan result)
414
364
415
365
for _ , url := range urls {
416
- go func (u string ) {
417
- resultChannel <- result{u , wc (u )}
418
- }(url )
366
+ go func () {
367
+ resultChannel <- result{url , wc (url )}
368
+ }()
419
369
}
420
370
421
371
for i := 0 ; i < len (urls); i++ {
0 commit comments