func fetch(url string, channel chan string, wg sync.WaitGroup) {
defer wg.Done()
resp, err := http.Get(url)
if (err != nil) || (resp.StatusCode != http.StatusOK) {
temp := fmt.Sprintf("Cannot fetch %s", url)
channel <- temp
fmt.Println("put one")
}
}
func preFetchAd(ad *Ad) error {
var wg sync.WaitGroup
count := 0
count = count + len(ad.Urls)
for _, urls := range ad.Urls2 {
count = count + len(urls)
}
c := make(chan string, count)
//fmt.Println(count)
errs := []string{}
for _, url := range ad.Urls {
wg.Add(1)
go fetch(url.L, c, wg)
}
for _, urls := range ad.Urls2 {
for _, url := range urls {
wg.Add(1)
go fetch(url.L, c, wg)
}
}
wg.Wait()
close(c)
for err := range c {
errs = append(errs, err)
}
if len(errs) == 0 {
return nil
}
e := strings.Join(errs, "\n")
return errors.New(e)
}
我先预算了个数,创建了足够缓存的 chan,但似乎依旧阻塞了?
1
orderc 2017-06-23 15:37:29 +08:00 1
// wg 要传指针
func fetch(url string, channel chan string, wg sync.WaitGroup) |
2
spice630 2017-06-23 15:38:08 +08:00
发个最小可重现代码,不然别人怎么帮你调?
|
4
reus 2017-06-23 16:08:48 +08:00
wg := new(sync.WaitGroup)
不然传参数的时候就是复制了,文档里说了不能复制 |
5
reus 2017-06-23 16:10:41 +08:00 1
用 go tool vet 也能发现错误
例如下面的代码 package main import "sync" func main() { var wg sync.WaitGroup foo(wg) } func foo(wg sync.WaitGroup) { } 执行 go tool vet a.go 输出 a.go:7: call of foo copies lock value: sync.WaitGroup contains sync.noCopy a.go:10: foo passes lock by value: sync.WaitGroup contains sync.noCopy |
6
tonyluj 2017-06-23 16:13:29 +08:00
善用 go vet,可以提前发现这些问题
main.go:5: fetch passes lock by value: sync.WaitGroup contains sync.noCopy |