dtttlua7165 2015-02-01 11:53
浏览 134
已采纳

模板中的bson.ObjectId

I have a struct with a bson.ObjectId type, for example something like this:

type Test struct {
     Id bson.ObjectId
     Name string
     Foo string
}

I want to render this in an html template

{{ Name }} {{ Food }}
<a href="/remove/{{ Id }}">Remove me</a>

But this obviously doesn't work since {{ Id }} would just return a ObjectId type, is there a way to convert this into a string inside the template?

Or do I have to do this when I pass data to the template.Execute?

  • 写回答

3条回答 默认 最新

  • dtz46697 2015-02-02 05:31
    关注

    Calling id.Hex() will return a string representation of the bson.ObjectId.

    This is also the default behavior if you try to marshal one bson.ObjectId to json string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题
  • ¥15 使用sql server语句实现下面两个实验(需要代码和运行结果截图)
  • ¥20 用web解决,要给我一个完整的网页,符合上述的要求
  • ¥20 求个sql server代码和结果的图 两道题
  • ¥15 银河麒麟操作系统无法使用U盘
  • ¥100 寻找:光电二极管电路设计服务
  • ¥15 YOLOv5改进后的结构图
  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率