当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > 详解Go开发Struct转换成map两种方式比较

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

2018年08月06日  | 移动技术网IT编程  | 我要评论

生态板 就选乐仙居,伊熙水,江苏镇江郦荟

最近做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形式转换的两倍。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网