cols, err := rows.Columns() // Remember to check err afterwards
vals := make([]interface{}, len(cols))
for i, _ := range cols {
vals[i] = new(sql.RawBytes)
}
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.
}
官方原文链接
官方的Demo是这样写的,最下面留了3行注释:
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.
请问这个use type introspection and type assertions to fetch the column into a typed variable到底要怎么实现?
查了诸多资料都没有找到答案。
网上大部分说的都是(string)value强转,可能这些说法都有些年代了,我现在这样写是不行的,会报错。
自答一下
接官方Demo,我是折腾了好久,这样绕了一圈才取到了能转string的值
也不知道有没有必要一定这样,是不是有简单点的方法,reflect和指针真是把我绕晕了