File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ * Never ever modifyread/use variable between different go routines.
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "time"
7
+ )
8
+
9
+ func main () {
10
+ links := []string {
11
+ "http://google.com" ,
12
+ "http://facebook.com" ,
13
+ "http://stackoverflow.com" ,
14
+ "http://golang.org" ,
15
+ "http://amazon.com" ,
16
+ }
17
+
18
+ c := make (chan string )
19
+
20
+ for _ , link := range links {
21
+ go checkLink (link , c )
22
+ }
23
+
24
+ for l := range c {
25
+ go func (link string ) {
26
+ time .Sleep (5 * time .Second )
27
+ checkLink (link , c )
28
+ }(l )
29
+ }
30
+ }
31
+
32
+ func checkLink (link string , c chan string ) {
33
+ _ , err := http .Get (link )
34
+ if err != nil {
35
+ fmt .Println (link , "might be down!" )
36
+ c <- link
37
+ return
38
+ }
39
+
40
+ fmt .Println (link , "is up!" )
41
+ c <- link
42
+ }
You can’t perform that action at this time.
0 commit comments