: first path segment in URL cannot contain colon


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\n", Location)
        resp, err := http.Get(Location)
        if err != nil {
        } 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)
            fmt.Println("\n", data.Keyword)
            fmt.Println("\n", data.Location)

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


2019/01/28 02:37:13 parse 
: 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"

func main(){

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.


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 “\nhttps://www.washingtonpost.com/news-sitemaps/politics.xml\n”

How to fix:

add this line before using Location in code:

Location = strings.TrimSpace(Location)

Answered By – Dmitry Harnitski

Answer Checked By – Cary Denson (GoLangFix Admin)

Leave a Reply

Your email address will not be published.