運(yùn)行此代碼時,我必須等待 10 秒才能打印 s.Locations,并等待 60 秒以上才能打印 n.Titles。是什么原因造成的?有關(guān)如何解決此問題的提示會很有幫助,即查看完成某些代碼行需要多長時間。Go 新手,所以不確定如何具體執(zhí)行此操作。我已確保關(guān)閉我的聯(lián)系。由于我計算機(jī)上的其他所有內(nèi)容加載速度都非??欤虼宋艺J(rèn)為通過 http.Get 訪問互聯(lián)網(wǎng)不會很慢。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 關(guān)注
- 111 瀏覽
添加回答
舉報
0/150
提交
取消