欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Go开发Struct转换成map两种方式比较

程序员文章站 2022-11-05 22:35:47
最近做go开发的时候接触到了一个新的orm第三方框架gorose,在使用的过程中,发现没有类似beego进行直接对struct结构进行操作的方法,有部分api是通过map进...

最近做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))
}

output:
map[proviece:jianxi com:广州兰博基尼 hos:zhonsndifneisnidnfie name:zhaondifnei company:sndifneinsifnienisn buys:shensinfienisnfieni age:23 postto:蓝鲸xxxxxxxx address:大沙地 school:广州第十五中学 city:zhongguoguanzhou sex:f id:98439 email:dashdisnin@126.com]
duration:250467

二、通过反射形式生成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))
}

output:
map[buys:shensinfienisnfieni name:zhaondifnei city:zhongguoguanzhou sex:f proviece:jianxi com:广州兰博基尼 id:98439 school:广州第十五中学 address:大沙地 age:23 postto:蓝鲸xxxxxxxx hos:zhonsndifneisnidnfie email:dashdisnin@126.com company:sndifneinsifnienisn]
duration:104239

结论

通过比较可以看出,通过反射的形式转换基本上是通过json形式转换的两倍。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。