dongyulan6251 2015-08-28 18:19
浏览 25

在Go中构建MRSS Feed

Im trying to get the result:

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">

with:

type RSS struct {
XMLName xml.Name `xml:"rss"`
Xmlns   string   `xml:"xmlns:media,attr"`
Version string  `xml:"version,attr"`
Channel Channel `xml:"channel"`
}

rss := &RSS{Version: "2.0", Xmlns:media: "http://search.yahoo.com/mrss"}

But I get a syntax error because of the colon. Without the ":media" there are no errors. How can I add that? Thanks.

  • 写回答

1条回答 默认 最新

  • douzhao4071 2015-08-28 22:45
    关注

    You've already done everything necessary to make it work, your composite literal is just wrong, you're using the xml attribute name rather than the field name. One of the main reason for annotation is to provide flexibility on your field names for some obvious reasons like the fact that lower cased fields aren't supported and the naming rules for json and xml aren't consistent with Go. Here is a working example of your code;

    type RSS struct {
    XMLName xml.Name `xml:"rss"`
    Xmlns   string   `xml:"xmlns:media,attr"`
    Version string  `xml:"version,attr"`
    Channel Channel `xml:"channel"`
    }
    
    rss := &RSS{Version: "2.0", Xmlns: "http://search.yahoo.com/mrss"}
    
    评论

报告相同问题?