diff --git a/chapter9/listing9.9_11/extractwordsmulti.go b/chapter9/listing9.9_11/extractwordsmulti.go index a6454bf..f09a00f 100644 --- a/chapter9/listing9.9_11/extractwordsmulti.go +++ b/chapter9/listing9.9_11/extractwordsmulti.go @@ -1,89 +1,86 @@ package main import ( - "fmt" - "github.com/cutajarj/ConcurrentProgrammingWithGo/chapter9/listing9.10" - "io" - "net/http" - "regexp" - "strings" + "fmt" + "github.com/cutajarj/ConcurrentProgrammingWithGo/chapter9/listing9.10" + "io" + "net/http" + "regexp" + "strings" ) func extractWords(quit <-chan int, pages <-chan string) <-chan string { - words := make(chan string) - go func() { - defer close(words) - wordRegex := regexp.MustCompile(`[a-zA-Z]+`) - moreData, page := true, "" - for moreData { - select { - case page, moreData = <-pages: - if moreData { - for _, word := range wordRegex.FindAllString(page, -1) { - words <- strings.ToLower(word) - } - } - case <-quit: - return - } - } - }() - return words + words := make(chan string) + go func() { + defer close(words) + wordRegex := regexp.MustCompile(`[a-zA-Z]+`) + moreData, page := true, "" + for moreData { + select { + case page, moreData = <-pages: + if moreData { + for _, word := range wordRegex.FindAllString(page, -1) { + words <- strings.ToLower(word) + } + } + case <-quit: + return + } + } + }() + return words } func downloadPages(quit <-chan int, urls <-chan string) <-chan string { - pages := make(chan string) - go func() { - defer close(pages) - moreData, url := true, "" - for moreData { - select { - case url, moreData = <-urls: - if moreData { - resp, _ := http.Get(url) - if resp.StatusCode != 200 { - panic("Server's error: " + resp.Status) - } - body, _ := io.ReadAll(resp.Body) - pages <- string(body) - resp.Body.Close() - } - case <-quit: - return - } - } - }() - return pages + pages := make(chan string) + go func() { + defer close(pages) + select { + case url, ok := <-urls: + if ok { + resp, _ := http.Get(url) + if resp.StatusCode != 200 { + panic("Server's error: " + resp.Status) + } + body, _ := io.ReadAll(resp.Body) + pages <- string(body) + resp.Body.Close() + } + case <-quit: + return + } + }() + return pages } func generateUrls(quit <-chan int) <-chan string { - urls := make(chan string) - go func() { - defer close(urls) - for i := 100; i <= 130; i++ { - url := fmt.Sprintf("https://rfc-editor.org/rfc/rfc%d.txt", i) - select { - case urls <- url: - case <-quit: - return - } - } - }() - return urls + urls := make(chan string) + go func() { + defer close(urls) + for i := 100; i <= 130; i++ { + url := fmt.Sprintf("https://rfc-editor.org/rfc/rfc%d.txt", i) + select { + case urls <- url: + case <-quit: + return + } + } + }() + return urls } const downloaders = 20 func main() { - quit := make(chan int) - defer close(quit) - urls := generateUrls(quit) - pages := make([]<-chan string, downloaders) - for i := 0; i < downloaders; i++ { - pages[i] = downloadPages(quit, urls) - } - results := extractWords(quit, listing9_10.FanIn(quit, pages...)) - for result := range results { - fmt.Println(result) - } -} + quit := make(chan int) + defer close(quit) + urls := generateUrls(quit) + pages := make([]<-chan string, downloaders) + for i := 0; i < downloaders; i++ { + pages[i] = downloadPages(quit, urls) + } + results := extractWords(quit, listing9_10.FanIn(quit, pages...)) + for result := range results { + fmt.Println(result) + } +} \ No newline at end of file