Golang gets a collection of fields in the map of an array (beego framework)

the data obtained from MySQL is as follows

[map[Uid:3 Avatar:12 Username:jack Level: Content:1212 Created_at:1540447583 Id:1] map[Username:jack Level: Content:1212 Created_at:1540447612 Id:2 Uid:3 Avatar:12]]

how to efficiently get the Uid collection in this array, and then use the Uid collection to look up the data in the user table and splice it together.

in addition, the demo I wrote myself is as follows


func (c *UserController) ChatMsgList() {
    o := orm.NewOrm()
    qs := o.QueryTable("chat_msg")

    var list []orm.Params
    qs.OrderBy("Id").Limit(2).Values(&list)
    beego.Info(list)
    var uids []int
    //listUid

    for key, value := range list {
        beego.Info(key)
        uids = append(uids, value["Uid"])
    }

    beego.Info(uids)

    //c.Data["list"] = list
    //
    //c.TplName = "user/chatMsgList.html"
    c.Data["json"] = list
    c.ServeJSON()
}
Oct.09,2021

can only loop.. After all, you use map to receive data
gorm.


is a little disappointed that so many golang programmers don't know how to write it.


you can use https://github.com/chenhg5/co. to provide convenient operations for various map/ arrays

Menu