Skip to content

Commit 426803f

Browse files
committed
go routines and channels
1 parent 909fd72 commit 426803f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

7.concurrency/Notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Never ever modifyread/use variable between different go routines.

7.concurrency/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)