我使用Go服务器创建了RESTful API的一个小实现。Golang服务器:发送带有可变列数的SQL查询结果的JSON
我提取网址查询参数(我知道这是不是安全,我会尽力稍后解决这个问题,但如果你有,甚至关于这个主题的建议,他们将是有益的)。
我有表名,期望的列和一些条件保存在3个变量中。 我使用这个查询:
rows, _ := db.Query(fmt.Sprintf("SELECT %s FROM %s WHERE %s", columns, table, conditions))
我想查询结果返回给我的前端,作为JSON。我有可变数量的未知列,所以我不能做到“标准”的方式。 我能想到的一个解决方案是从查询结果和rows.Columns()中“手动”构建一个JSON字符串。
,但我想这样做在使用类似的可变参数的接口之类的东西更sofisticated方式。问题是,即使尝试了很多,我仍然不明白它是如何工作的。
我尝试使用下面的代码
cols, err := rows.Columns() // Get the column names; remember to check err
vals := make([]sql.RawBytes, len(cols)) // Allocate enough values
ints := make([]interface{}, len(cols)) // Make a slice of []interface{}
for i := range ints {
vals[i] = &ints[i] // Copy references into the slice
}
for rows.Next() {
err := rows.Scan(vals...)
// Now you can check each element of vals for nil-ness,
// and you can use type introspection and type assertions
// to fetch the column into a typed variable.
}
从this tutorial,但它不工作,我得到这样的错误
cannot use &ints[i] (type *interface {}) as type sql.RawBytes in assignment
即使它会工作,我不明白它。
有没有人有这个好的解决方案?一些解释会很好。
非常感谢。