-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.go
82 lines (67 loc) · 1.91 KB
/
thread.go
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
/*
第5章 並行プログラミング―ゴルーチンとチャネルを使いこなす
https://gihyo.jp/dev/feature/01/go_4beginners/0005
ゴルーチン:軽量な並行処理プログラミング
チャネル:複数のゴルーチン間でデータのやり取りをする
*/
package main
import (
"fmt"
"log"
"net/http"
)
/*
func sampleSelectCase() {
ch1 := make(chan string)
ch2 := make(chan string)
for {
// どれか1つcaseが実行されない限りは、select文はブロックする
select {
case c1 := <-ch1:
// ch1からデータを読み出した時に実行
case c2 := <-ch2:
// ch2からデータを読み出した時に実行
case ch2 <- "c":
// ch2にデータを書き込んだ時に実行
default:
// caceが実行されなかった場合に実行
// defaultの実行が終わるとselect文の処理が終わるため、select文がブロックされなくなる
}
}
}
*/
func getStatus(urls []string) <-chan string {
statusChan := make(chan string)
for _, url := range urls {
// wait.Add(1)
go func(url string) {
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
// fmt.Println(url, res.Status)
statusChan <- res.Status
// wait.Done()
}(url)
}
return statusChan
}
func main() {
// wait := new(sync.WaitGroup)
urls := []string{
"https://keiba.to48.org/",
"https://note.com/cybermamecan/",
"https://egingerz.to48.org/",
}
statusChan := getStatus(urls)
// main が終了しないように待ち合わせる
// time.Sleep(time.Second)
// wait.Wait()
// ゴルーチンでstatusChanに値が書き込まれるまで、main()の中では値を読み出すことが出来ない。
// ここではstatusChanの読み出しが3回完了するまで処理がブロックされる。
// よってWait処理は必要ない。
for i := 0; i < len(urls); i++ {
fmt.Println(<-statusChan)
}
}