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"}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离