drmticpet66231422 2012-09-20 05:44
浏览 27
已采纳

在Go中将地图追加到地图

I am trying to build a map[string]map[string]string which will look something like this:

{ "notes": 
    {
    "Title":note.Title,
    "Body":note.Body,
    },
    {
    "Title":note.Title,
    "Body":note.Body,
    },
    {
    "Title":note.Title,
    "Body":note.Body,
    },
}

from a struct (notes) of structs (note)

I have thought of doing it like this:

for _, note := range notes {
        thisNote := map[string]string{
            "Title":note.Title,
            "Body":note.Body,
        }

        content["notes"] = append(content["notes"], thisNote)
}

But obviously that is not going to work because I am trying to append a map to a map rather than a slice.

Is there a really easy solution to this that I am missing?

  • 写回答

2条回答 默认 最新

  • dongyang7152 2012-09-20 12:12
    关注

    I'm pretty sure you can use a struct like this instead since mustache receives the data as an interface{}

    func handler(w http.ResponseWriter, r *http.Request) {
        var data struct {
            Notes []*Note
        }
    
        notes := ...
        data.Notes = notes
        tmpl := ...
        templ.Render(data, w)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?