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

Here's my code ( part of it ) :

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
", 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("


", idx)
            fmt.Println("
", data.Keyword)
            fmt.Println("
", data.Location)
        }
    }

Now, when I run this code I get this output :


https://www.washingtonpost.com/news-sitemaps/politics.xml

2019/01/28 02:37:13 parse 
https://www.washingtonpost.com/news-sitemaps/politics.xml
: first path segment in URL cannot contain colon
exit status 1

I read a few posts and did some experiment myself, like I made another file with the following code

package main

import ("fmt"
    "net/url")

func main(){
    fmt.Println(url.Parse("https://www.washingtonpost.com/news-sitemaps/politics.xml"))
}

And it didn't throw any error, so I understand the error is not with the url .

Now, I just started learning Go using sentdex's tutorials , a few hours ago and so don't have much idea as of now. Here's the video link

Thanks and regards. Temporarya

The problem here is that Location has whitespace prefix and suffix so string is not valid URL. Unfortunately, error message does not help to see that.

How to detect:

I typically use %q fmt helper that wraps string into parentheses:

fmt.Printf("%q", Location) 

Will be printed as " https://www.washingtonpost.com/news-sitemaps/politics.xml "

How to fix:

add this line before using Location in code:

Location = strings.TrimSpace(Location)