@@ -11,13 +11,13 @@ package concurrency
11
11
type WebsiteChecker func (string ) bool
12
12
13
13
func CheckWebsites (wc WebsiteChecker , urls []string ) map [string ]bool {
14
- results := make (map [string ]bool )
14
+ results := make (map [string ]bool )
15
15
16
- for _ , url := range urls {
17
- results[url] = wc (url)
18
- }
16
+ for _ , url := range urls {
17
+ results[url] = wc (url)
18
+ }
19
19
20
- return results
20
+ return results
21
21
}
22
22
```
23
23
@@ -36,35 +36,35 @@ Here's the test they've written:
36
36
package concurrency
37
37
38
38
import (
39
- " reflect"
40
- " testing"
39
+ " reflect"
40
+ " testing"
41
41
)
42
42
43
43
func mockWebsiteChecker (url string ) bool {
44
- if url == " waat://furhurterwe.geds" {
45
- return false
46
- }
47
- return true
44
+ if url == " waat://furhurterwe.geds" {
45
+ return false
46
+ }
47
+ return true
48
48
}
49
49
50
50
func TestCheckWebsites (t *testing .T ) {
51
- websites := []string {
52
- " http://google.com" ,
53
- " http://blog.gypsydave5.com" ,
54
- " waat://furhurterwe.geds" ,
55
- }
56
-
57
- want := map [string ]bool {
58
- " http://google.com" : true ,
59
- " http://blog.gypsydave5.com" : true ,
60
- " waat://furhurterwe.geds" : false ,
61
- }
62
-
63
- got := CheckWebsites (mockWebsiteChecker, websites)
64
-
65
- if !reflect.DeepEqual (want, got) {
66
- t.Fatalf (" Wanted %v , got %v " , want, got)
67
- }
51
+ websites := []string {
52
+ " http://google.com" ,
53
+ " http://blog.gypsydave5.com" ,
54
+ " waat://furhurterwe.geds" ,
55
+ }
56
+
57
+ want := map [string ]bool {
58
+ " http://google.com" : true ,
59
+ " http://blog.gypsydave5.com" : true ,
60
+ " waat://furhurterwe.geds" : false ,
61
+ }
62
+
63
+ got := CheckWebsites (mockWebsiteChecker, websites)
64
+
65
+ if !reflect.DeepEqual (want, got) {
66
+ t.Fatalf (" Wanted %v , got %v " , want, got)
67
+ }
68
68
}
69
69
```
70
70
@@ -81,24 +81,24 @@ effect of our changes.
81
81
package concurrency
82
82
83
83
import (
84
- " testing"
85
- " time"
84
+ " testing"
85
+ " time"
86
86
)
87
87
88
88
func slowStubWebsiteChecker (_ string ) bool {
89
- time.Sleep (20 * time.Millisecond )
90
- return true
89
+ time.Sleep (20 * time.Millisecond )
90
+ return true
91
91
}
92
92
93
93
func BenchmarkCheckWebsites (b *testing .B ) {
94
- urls := make ([]string , 100 )
95
- for i := 0 ; i < len (urls); i++ {
96
- urls[i] = " a url"
97
- }
98
-
99
- for i := 0 ; i < b.N ; i++ {
100
- CheckWebsites (slowStubWebsiteChecker, urls)
101
- }
94
+ urls := make ([]string , 100 )
95
+ for i := 0 ; i < len (urls); i++ {
96
+ urls[i] = " a url"
97
+ }
98
+
99
+ for i := 0 ; i < b.N ; i++ {
100
+ CheckWebsites (slowStubWebsiteChecker, urls)
101
+ }
102
102
}
103
103
```
104
104
@@ -158,15 +158,15 @@ package concurrency
158
158
type WebsiteChecker func (string ) bool
159
159
160
160
func CheckWebsites (wc WebsiteChecker , urls []string ) map [string ]bool {
161
- results := make (map [string ]bool )
161
+ results := make (map [string ]bool )
162
162
163
- for _ , url := range urls {
164
- go func () {
165
- results[url] = wc (url)
166
- }()
167
- }
163
+ for _ , url := range urls {
164
+ go func () {
165
+ results[url] = wc (url)
166
+ }()
167
+ }
168
168
169
- return results
169
+ return results
170
170
}
171
171
```
172
172
@@ -228,17 +228,17 @@ import "time"
228
228
type WebsiteChecker func (string ) bool
229
229
230
230
func CheckWebsites (wc WebsiteChecker , urls []string ) map [string ]bool {
231
- results := make (map [string ]bool )
231
+ results := make (map [string ]bool )
232
232
233
- for _ , url := range urls {
234
- go func () {
235
- results[url] = wc (url)
236
- }()
237
- }
233
+ for _ , url := range urls {
234
+ go func () {
235
+ results[url] = wc (url)
236
+ }()
237
+ }
238
238
239
- time.Sleep (2 * time.Second )
239
+ time.Sleep (2 * time.Second )
240
240
241
- return results
241
+ return results
242
242
}
243
243
```
244
244
@@ -266,23 +266,23 @@ To fix this:
266
266
package concurrency
267
267
268
268
import (
269
- " time"
269
+ " time"
270
270
)
271
271
272
272
type WebsiteChecker func (string ) bool
273
273
274
274
func CheckWebsites (wc WebsiteChecker , urls []string ) map [string ]bool {
275
- results := make (map [string ]bool )
275
+ results := make (map [string ]bool )
276
276
277
- for _ , url := range urls {
278
- go func (u string ) {
279
- results[u] = wc (u)
280
- }(url)
281
- }
277
+ for _ , url := range urls {
278
+ go func (u string ) {
279
+ results[u] = wc (u)
280
+ }(url)
281
+ }
282
282
283
- time.Sleep (2 * time.Second )
283
+ time.Sleep (2 * time.Second )
284
284
285
- return results
285
+ return results
286
286
}
287
287
```
288
288
@@ -405,26 +405,26 @@ package concurrency
405
405
406
406
type WebsiteChecker func (string ) bool
407
407
type result struct {
408
- string
409
- bool
408
+ string
409
+ bool
410
410
}
411
411
412
412
func CheckWebsites (wc WebsiteChecker , urls []string ) map [string ]bool {
413
- results := make (map [string ]bool )
414
- resultChannel := make (chan result)
413
+ results := make (map [string ]bool )
414
+ resultChannel := make (chan result)
415
415
416
- for _ , url := range urls {
417
- go func (u string ) {
418
- resultChannel <- result{u, wc (u)}
419
- }(url)
420
- }
416
+ for _ , url := range urls {
417
+ go func (u string ) {
418
+ resultChannel <- result{u, wc (u)}
419
+ }(url)
420
+ }
421
421
422
- for i := 0 ; i < len (urls); i++ {
423
- result := <- resultChannel
424
- results[result.string ] = result.bool
425
- }
422
+ for i := 0 ; i < len (urls); i++ {
423
+ result := <- resultChannel
424
+ results[result.string ] = result.bool
425
+ }
426
426
427
- return results
427
+ return results
428
428
}
429
429
```
430
430
0 commit comments