// *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. Azure CLI 简单入门

    Azure CLI 是什么 Azure 命令行接口 (CLI) 是用于管理 Azure 资源的 Microsoft 跨平台命令行体验. Azure CLI 易于学习,是构建适用于 Azure 资源的自 ...

  2. Java集合03——你不得不了解的Map

    Map 在面试中永远是一个绕不开的点,本文将详细讲解Map的相关内容.关注公众号「Java面典」了解更多 Java 知识点. Map Map 是一个键值对(key-value)映射接口: 映射中不能包 ...

  3. Simulink仿真入门到精通(五) Simulink模型的仿真

    5.1 模型的配置仿真 由各种模块所构建的可视化逻辑连接,只是模型的外在表现,模型仿真的核心驱动器是被称作解算器(Solver)的组件,相当于Simulink仿真过程的心脏,驱动着模型仿真,它在每一个 ...

  4. web自动化原理

    在说原理之前我想说下我所理解的selenium: (1).支持多语言,多平台,多浏览器 (2).它是一个工具包 (3).提供所有的网页操作api,是一个功能库 通过selenium来实现web自动化, ...

  5. vscode C++ 程序 windows

    vscode 1.42.1 OS: windows 7 x64 1. vscode, cpp extension 本文直接跳过 vscode 安装, Cpp tools 安装 2. MinGw 安装及 ...

  6. 【BIM】BIMFACE中创建矢量文本[下篇]

    背景 在上一篇文章中,我们通过THREEJS创建了矢量文本,并添加到了BIMFACE场景中,但是仅仅加入到场景中并不是我们的目的,我们的目的是把这种矢量文本加到指定的构件或者空间上,以此标识该构件或空 ...

  7. linux工具集

    1.ag:比grep.ack更快的递归搜索文件内容 安装: 1:首先在linux创建个sh文件->ag.sh 2:在ag.sh里面输入如下内容并保存 set -x TEMP_DIR=$(mkte ...

  8. 在Centos系统中基于PowerDNS和Poweradmin自建域名解析服务器替代DnsPod

    本文讲述了我在Centos 7系统(其他版本的Centos未尝试)中基于PowerDNS和poweradmin自建域名解析服务器替代DnsPod的过程.通过本文所述方法,可以建立权威域名解析服务器的m ...

  9. python的C扩展调用,使用原生的python-C-Api

    1.在文件第一行包含python调用扩展的头文件 #include <Python.h> 2.用原生C写好需要调用的函数 int add_one(int a){ ; } 3.用python ...

  10. 【分布式锁】05-使用Redisson中Semaphore和CountDownLatch原理

    前言 前面已经写了Redisson大多的内容,我们再看看Redisson官网共有哪些组件: image.png 剩下还有Semaphore和CountDownLatch两块,我们就趁热打铁,赶紧看看R ...