Create a Map in Golang from database Rows

后端 未结 5 2066
别那么骄傲
别那么骄傲 2020-12-14 07:14

Basically after doing a query I\'d like to take the resulting rows and produce a []map[string]interface{}, but I do not see how to do this with the API since th

5条回答
  •  情书的邮戳
    2020-12-14 08:08

    Look at using sqlx, which can do this a little more easily than the standard database/sql library:

    places := []Place{}
    err := db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
    if err != nil {
        fmt.Printf(err)
        return
    }
    

    You could obviously replace []Place{} with a []map[string]interface{}, but where possible it is better to use a struct if you know the structure of your database. You won't need to undertake any type assertions as you might on an interface{}.

提交回复
热议问题