dpzzkfb1244 2019-05-10 21:34
浏览 102

如何使用两组数据修复golang嵌套模板

I want to get two data structs into a golang nested template, I've created a struct like this for 'url' and for 'user' data like so,

type url struct {
    id    string
    Userid    string
    Long_url  string
    Short_url string
}
type user struct {
    Email        string
    First_name   string
    Last_name    string
    Password     []byte
}

I want to get the two sets of data into a nested golang template. The user data is just one set of data and the url struct will have many rows of data.

My idea is that I would create another struct like so,

   type Data struct {
        UU []url
        User []user
    }

and then do the following,

  bb := []url{urls}
  tu := []user{u}
  data := &Data{bb, tu}
 tp.ExecuteTemplate(w, "form", data)

the output of the url data is like this,

[{2 123 https://confluence.expedia.biz/display/INFRAENG/Graphite+Infrastructure short2} {1 123 https://tour.golang.org/moretypes/13 short}]

the error I get is as follows:

error:  "cannot use urls (type []url) as type url in array or slice literal"

I'm really not sure how to progress, can you help?

  • 写回答

1条回答 默认 最新

  • duanbage2161 2019-05-12 03:04
    关注

    https://goplay.space/#og-isDSo2oW

    Always create a test case on playground to test your problem and to share with others. Hope this helps.

    type Data struct {
        url  // embedded
        User []user
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误