zoukankan      html  css  js  c++  java
  • golang 开发 Struct 转换成 map 两种方式比较

    原文链接:https://www.jianshu.com/p/81c4304f6d1b

    最近做Go开发的时候接触到了一个新的orm第三方框架gorose,在使用的过程中,发现没有类似beego进行直接对struct结构进行操作的方法,有部分API是通过map进行数据库相关操作,那么就需要我们把struct转化成map,下面是是我尝试两种不同struct转换成map的方法:

    mport (
        "encoding/json"
        "fmt"
        "reflect"
        "time"
    )
    
    type Persion struct {
        Id       int
        Name     string
        Address  string
        Email    string
        School   string
        City     string
        Company  string
        Age      int
        Sex      string
        Proviece string
        Com      string
        PostTo   string
        Buys     string
        Hos      string
    }
    
    func main() {
        StructToMapViaJson()
        //StructToMapViaReflect()
    }
    
    func StructToMapViaJson() {
        m := make(map[string]interface{})
        t := time.Now()
        person := Persion{
            Id:       98439,
            Name:     "zhaondifnei",
            Address:  "大沙地",
            Email:    "dashdisnin@126.com",
            School:   "广州第十五中学",
            City:     "zhongguoguanzhou",
            Company:  "sndifneinsifnienisn",
            Age:      23,
            Sex:      "F",
            Proviece: "jianxi",
            Com:      "广州兰博基尼",
            PostTo:   "蓝鲸XXXXXXXX",
            Buys:     "shensinfienisnfieni",
            Hos:      "zhonsndifneisnidnfie",
        }
        j, _ := json.Marshal(person)
        json.Unmarshal(j, &m)
        fmt.Println(m)
        fmt.Println(time.Now().Sub(t))
    }

    一、通过struct转json,json转成map

    func StructToMapViaJson() {
        m := make(map[string]interface{})
        t := time.Now()
        person := Persion{
            Id:       98439,
            Name:     "zhaondifnei",
            Address:  "大沙地",
            Email:    "dashdisnin@126.com",
            School:   "广州第十五中学",
            City:     "zhongguoguanzhou",
            Company:  "sndifneinsifnienisn",
            Age:      23,
            Sex:      "F",
            Proviece: "jianxi",
            Com:      "广州兰博基尼",
            PostTo:   "蓝鲸XXXXXXXX",
            Buys:     "shensinfienisnfieni",
            Hos:      "zhonsndifneisnidnfie",
        }
        j, _ := json.Marshal(person)
        json.Unmarshal(j, &m)
        fmt.Println(m)
        fmt.Printf("duration:%d", time.Now().Sub(t))
    }

    二、通过反射形式生成map

    func StructToMapViaReflect() {
        m := make(map[string]interface{})
        t := time.Now()
        person := Persion{
            Id:       98439,
            Name:     "zhaondifnei",
            Address:  "大沙地",
            Email:    "dashdisnin@126.com",
            School:   "广州第十五中学",
            City:     "zhongguoguanzhou",
            Company:  "sndifneinsifnienisn",
            Age:      23,
            Sex:      "F",
            Proviece: "jianxi",
            Com:      "广州兰博基尼",
            PostTo:   "蓝鲸XXXXXXXX",
            Buys:     "shensinfienisnfieni",
            Hos:      "zhonsndifneisnidnfie",
        }
        elem := reflect.ValueOf(&person).Elem()
        relType := elem.Type()
        for i := 0; i < relType.NumField(); i++ {
            m[relType.Field(i).Name] = elem.Field(i).Interface()
        }
        fmt.Println(m)
        fmt.Printf("duration:%d", time.Now().Sub(t))
    }
  • 相关阅读:
    [导入]mootools框架【二】Core篇: 主要方法测试实例
    公司招聘中不能说的秘密 【转载】
    国外10个ASP.Net C#下的开源CMS
    [导入][Flash开发笔记] List控件删除指定label或data的项
    [导入]用C#截取指定长度的中英文混合字符串 改进版
    今天小侄子出生,想了一天的名字
    一个正则表达式的解释
    今日小收获
    昨天的事
    两点东西
  • 原文地址:https://www.cnblogs.com/wangjq19920210/p/11600479.html
Copyright © 2011-2022 走看看