// *sql.Rows 转换为 []map[string]interface{}类型
func rows2maps(rows *sql.Rows) (res []map[string]interface{}) {

    defer rows.Close()
    cols, _ := rows.Columns()
    cache := make([]interface{}, len(cols))
    // 为每一列初始化一个指针
    for index, _ := range cache {
        var a interface{}
        cache[index] = &a
    }

    for rows.Next() {
        rows.Scan(cache...)
        row := make(map[string]interface{})
        for i, val := range cache {

            // 处理数据类型
            v := *val.(*interface{})
            switch v.(type) {
            case []uint8:
                v = string(v.([]uint8))
            case nil:
                v = ""
            }
            row[cols[i]] = v
        }

        res = append(res, row)
    }

    return res
}

sql.Rows 转换为 []map[string]interface{} 类型的更多相关文章

  1. GO学习-(38) Go语言结构体转map[string]interface{}的若干方法

    结构体转map[string]interface{}的若干方法 本文介绍了Go语言中将结构体转成map[string]interface{}时你需要了解的"坑",也有你需要知道的若 ...

  2. go语言解析 map[string]interface{} 数据格式

    原文:https://blog.csdn.net/Nick_666/article/details/79801914 map记得分配内存 解析出来的int类型会变成float64类型 注意判断不为ni ...

  3. is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface | mongodb在windows和Linux导出出错

    执行mongoexport命令的时候 mongoexport --csv -f externalSeqNum,paymentId --host 127.0.0.1:27017 -d liveX -c ...

  4. This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa

    This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interf ...

  5. map[string]interface{} demo

    package main import ( "encoding/json" "fmt" "reflect" ) func demo1() { ...

  6. MyBatis 返回Map<String,Object>类型

    <!-- 导出所有数据 --> <select id="exportAll" resultMap="map"> SELECT t1.ME ...

  7. com.alibaba.fastjson把JSONObject转换为Map<String, String>对象

    https://www.cnblogs.com/fomeiherz/p/6351287.html JSONObject obj = new JSONObject();{obj.put("ke ...

  8. mybatis foreach Map(String,List)类型

    <select id="queryList" resultType="com.performancetest.modules.ptest.entity.Stress ...

  9. golang语言sql Rows转化保存成map

    func DoQuery(db *sql.DB, sqlInfo string, args ...interface{}) ([]map[string]interface{}, error) { ro ...

随机推荐

  1. 快速上手 Python 命令行模块 Click

    关于Click? 说下 Click 模块是干啥的,简单说,它就是把我们的 Python 脚本的一些函数,通过 添加带有 Click 关键字的装饰器进行装饰进而将函数调用的形式转化为命令行传参的形式然后 ...

  2. flask 对于邮件url进行一个加密防止爆破

    注册表单 from app.modles import User class registerForm(FlaskForm): nicheng = StringField('昵称',validator ...

  3. abp(net core)+easyui+efcore实现仓储管理系统——入库管理之四(四十)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  4. python基础学习day03

    基础数据类型总览 why:机器无法像人一样分编各种类型 int(数字) str(字符串)作用:存储少量信息. '12','我和你','qw' bool值 作用:判断真假 True False list ...

  5. CSS样式命名

    CSS样式命名    说明网页公共命名#wrapper    页面外围控制整体布局宽度#container或#content    容器,用于最外层#layout    布局#head, #heade ...

  6. C++总结之template

    函数模板 我们可以把函数模板当做一种特殊的函数,里面的参数类型可以是任意类型,这样的话我们就可以减少重复定义,从而让这个函数模板自动适应不同的参数类型,也就是说函数可以适应多种类型的参数,例如doub ...

  7. webStorm -> Version Control _> Repository -> Filter By User 查看svn日志

    webStorm -> Version Control _> Repository -> Filter By User 查看svn日志

  8. try_catch_return

    1.情况一(try中有return,finally中没有return): public class TryTest{ public static void main(String[] args){ S ...

  9. Gitblit无法查看单个文件解决方案

    一个简单的解决方案是在reference.properties中设置: web.mountParameters = false 在这种情况下,您完全避免了该问题,因为项目名称,分支和文件名作为查询字符 ...

  10. BigDecimal介绍及BigDecimal实现四舍五入

    BigDecimal介绍及BigDecimal实现四舍五入 BigDecimal是什么? 我们知道float最大精度是7-8位有效数字,而double的最大精度是16-17位有效数字,那么大于16位的 ...