代码如下
```
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/PuerkitoBio/goquery"
)
func urllist(url string) ([]string, error) {
var backurl []string
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("getting %s: %s", url, resp.Status)
}
fmt.Println(url)
dom, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
dom.Find("a").Each(func(i int, selection *goquery.Selection) {
href, true := selection.Attr("href")
if true {
link, _ := resp.Request.URL.Parse(href)
backurl = append(backurl, link.String())
}
})
return backurl, nil
}
func main() {
firsturl := make(chan []string)
uniqurl := make(chan string)
go func() { firsturl <- os.Args[1:] }()
for i := 0; i < 20; i++ {
go func() {
for link := range uniqurl {
foundlink, err := urllist(link)
if err != nil {
log.Print(err)
}
go func() { firsturl <- foundlink }()
}
}()
}
seen := make(map[string]bool)
for list := range firsturl {
for _, link := range list {
if !seen[link] {
seen[link] = true
uniqurl <- link
}
}
}
}
```
执行:go run pachong.go http://news.baidu.com
报错:
```goroutine 36 [running]:
net/url.(*URL).String(0x0)
/usr/local/Cellar/go/1.17/libexec/src/net/url/url.go:813 +0x40
main.urllist.func1(0xc00019e340, 0xc000660780)
/Users/baby/Desktop/go/study/goroutine/goquery/pachong.go:31 +0x6b
github.com/PuerkitoBio/goquery.(*Selection).Each(0xc001084270, 0xc00042ff28)
/Users/baby/Library/go/pkg/mod/github.com/!puerkito!bio/goquery@v1.8.0/iteration.go:10 +0x46
main.urllist({0xc0001c8b70, 0x1c})
/Users/baby/Desktop/go/study/goroutine/goquery/pachong.go:27 +0x15e
main.main.func2()
/Users/baby/Desktop/go/study/goroutine/goquery/pachong.go:45 +0x68
created by main.main
/Users/baby/Desktop/go/study/goroutine/goquery/pachong.go:43 +0x9f
exit status 2
```
求指点
更多评论
测试了一下,不考虑代码逻辑,感觉都是因为没有进行相应的判断出现了空指针赋值。![微信截图_20220424162511.png](https://static.golangjob.cn/220424/d9e4d84407e2ec7d32b0ef5ec3486db7.png)
修改后我跑了一遍,再没出现错误退出
#1