亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的代碼需要這么長時間才能返回結果?

為什么我的代碼需要這么長時間才能返回結果?

Go
翻閱古今 2023-07-10 10:58:03
運行此代碼時,我必須等待 10 秒才能打印 s.Locations,并等待 60 秒以上才能打印 n.Titles。是什么原因造成的?有關如何解決此問題的提示會很有幫助,即查看完成某些代碼行需要多長時間。Go 新手,所以不確定如何具體執行此操作。我已確保關閉我的聯系。由于我計算機上的其他所有內容加載速度都非???,因此我認為通過 http.Get 訪問互聯網不會很慢。package mainimport (    "encoding/xml"    "fmt"    "io/ioutil"    "net/http"    "strings")// SitemapIndex is the root xmltype SitemapIndex struct {    Locations []string `xml:"sitemap>loc"`}// News is the individual categoriestype News struct {    Titles    []string `xml:"url>news>title"`    Keywords  []string `xml:"url>news>keywords"`    Locations []string `xml:"url>loc"`}// NewsMap is thetype NewsMap struct {    Keywords string    Location string}func main() {    var s SitemapIndex    var n News    // np := make(map[string]NewsMap)    resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")    bytes, _ := ioutil.ReadAll(resp.Body)    xml.Unmarshal(bytes, &s)    resp.Body.Close()    for i := range s.Locations {        s.Locations[i] = strings.TrimSpace(s.Locations[i])    }    fmt.Println(s.Locations) // slice of data    for _, Location := range s.Locations {        resp, _ := http.Get(Location)        bytes, _ := ioutil.ReadAll(resp.Body)        xml.Unmarshal(bytes, &n)        resp.Body.Close()    }    fmt.Println(n.Titles)}我得到了輸出,但 s.Locations 需要等待 10 秒,n.Titles 需要等待 60 秒以上
查看完整描述

目前暫無任何回答

  • 0 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號