When running this code I have to wait 10 seconds for s.Locations to print and 60+ seconds for n.Titles to print. What is causing this?
Tips on how to troubleshoot this would be helpful i.e. seeing how long it takes for certain lines of code to complete. New to Go so not sure how to exactly do this.
I've made sure I close my connections. Since everything else on my computer loads blazing fast I don't think to access the internet via http.Get should be slow.
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// SitemapIndex is the root xml
type SitemapIndex struct {
Locations []string `xml:"sitemap>loc"`
}
// News is the individual categories
type News struct {
Titles []string `xml:"url>news>title"`
Keywords []string `xml:"url>news>keywords"`
Locations []string `xml:"url>loc"`
}
// NewsMap is the
type 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)
}
I get the output but I have to wait 10 seconds for s.Locations and 60+ seconds for n.Titles