我正在输入 URL 的“stdin”行,例如: $ echo -e ' https://golang.org \nhttps://godoc.org\nhttps://golang.org' | 去跑 1.go 。任务是从每个 WEB 页中获取单词“Go”的编号。但是我不能启动超过 5 个 goroutines 并且只能使用标准库这是我的代码:
package main
import (
"fmt"
"net/http"
"bufio"
"os"
"regexp"
"io/ioutil"
"time"
)
func worker(id int, jobs<-chan string, results chan<-int) {
t0 := time.Now()
for url := range jobs {
resp, err := http.Get(url)
if err != nil {
fmt.Println("problem while opening url", url)
results<-0
//continue
}
defer resp.Body.Close()
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}
regExp:= regexp.MustCompile("Go")
matches := regExp.FindAllStringIndex(string(html), -1)
t1 := time.Now()
fmt.Println("Count for", url, ":", len(matches), "Elapsed time:",
t1.Sub(t0), "works id", id)
results<-len(matches)
}
}
func main(){
scanner := bufio.NewScanner(os.Stdin)
jobs := make(chan string, 100)
results := make(chan int, 100)
t0 := time.Now()
for w:= 0; w<5; w++{
go worker(w, jobs, results)
}
var tasks int = 0
res := 0
for scanner.Scan() {
jobs <- scanner.Text()
tasks ++
}
close(jobs)
for a := 1; a <= tasks; a++ {
res+=<-results
}
close(results)
t2 := time.Now()
fmt.Println("Total:",res, "Elapsed total time:", t2.Sub(t0) );
}
我认为它可以工作,直到我将超过 5 个 URL(其中一个不正确)传递给标准输入。输出是:
goroutine 9 [running]:
panic ...
显然,已经启动了额外的 gooutnes。如何解决?可能有更方便的方法来限制 goroutine 的数量?