我想创建一个多维数组,如下所示:[string][int] = Struct我编写了以下代码块。当我尝试将结构类型分配给映射时,出现以下错误:无法在赋值 go 中使用事件(类型 *models.Event)作为类型 map[int]models.Event这是代码块://Get funcfunc (c *EventController) Get() { days := [7]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} m := make(map[string][]map[int]models.Event) for _, day := range days { var i = 0 o := orm.NewOrm() qs := o.QueryTable("events") qs.OrderBy("id") qs.Filter("days__name", day) var events []*models.Event num, err := o.QueryTable("events").RelatedSel().All(&events) if err == nil { fmt.Printf("%d events read\n", num) for _, event := range events { m[day][i] = event i++ } } else { fmt.Println(err) } } c.Data["json"] = m // c.TplName = "events.tpl" c.ServeJSON()}这里有什么问题吗?
1 回答
慕沐林林
TA贡献2016条经验 获得超9个赞
正如 Marc 所定义的,我更新了代码块,如下所示。现在它完美运行了!
//Get func
func (c *EventController) Get() {
days := [7]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
m := make(map[string][]*models.Event)
for _, day := range days {
o := orm.NewOrm()
var events []*models.Event
num, err := o.QueryTable("events").Filter("day__name__exact", day).RelatedSel().All(&events)
fmt.Print("Day: ", day, " Count:", num, "\n")
if err != nil {
fmt.Printf("%d posts read\n", num)
}
m[day] = events
}
c.Data["json"] = m
// c.TplName = "events.tpl"
c.ServeJSON()
}
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报
0/150
提交
取消