小编tem*_*rya的帖子

: URL 中的第一个路径段不能包含冒号

这是我的代码(其中的一部分):

type SitemapIndex struct {
    // Locations []Location `xml:"sitemap"`
    Locations []string `xml:"sitemap>loc"`
}

~~~ SNIP ~~~
func main(){
    var s SitemapIndex
    resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
    bytes, _ := ioutil.ReadAll(resp.Body)
    xml.Unmarshal(bytes, &s)
    for _, Location := range s.Locations {
        fmt.Printf("%s\n", Location)
        resp, err := http.Get(Location)
        if err != nil {
            log.Fatal(err)
        } else {
            bytes, _ := ioutil.ReadAll(resp.Body)
            xml.Unmarshal(bytes, &n)
            for idx := range n.Titles {
                newsMap[n.Titles[idx]] = NewsMap{n.Keywords[idx], n.Locations[idx]}
            }
        }
        for idx, data := range newsMap {
            fmt.Println("\n\n\n", idx) …
Run Code Online (Sandbox Code Playgroud)

go

4
推荐指数
2
解决办法
1万
查看次数

我如何处理goroutines中的恐慌?

我很新golang.所以,请把剑给我(如果可能的话).

我试图通过在这里学习教程从网上获取数据

现在,教程一切顺利,但我想检查边缘情况和错误处理(只是为了彻底了解我对语言的新学习,不想成为具有半生不熟的知识的人).

这是我的游乐场代码.

在询问之前我看过很多参考资料,例如:

  1. 去博客推迟,恐慌和恢复
  2. 处理goroutines中的恐慌
  3. 如何-应该-I-写够程

还有一些,但我无法弄明白.

这是代码,以防您不想去操场(原因尚不为人所知):

// MakeRequest : Makes requests concurrently
func MakeRequest(url string, ch chan<- string, wg *sync.WaitGroup) {
    start := time.Now()
    resp, err := http.Get(url)
    defer func() {
        resp.Body.Close()
        wg.Done()
            if r := recover(); r != nil {
                fmt.Println("Recovered in f", r)
            }
    }()
    if err != nil {
        fmt.Println(err)
        panic(err)
    }
    secs := time.Since(start).Seconds()
    body, _ := ioutil.ReadAll(resp.Body)

    ch <- fmt.Sprintf("%.2f elapsed with response …
Run Code Online (Sandbox Code Playgroud)

go

2
推荐指数
1
解决办法
451
查看次数

标签 统计

go ×2