Go开发Struct转换成map两种方式比较
最近做Go开发的时候接触到了一个新的orm第三方框架gorose,在使用的过程中,发现没有类似beego进行直接对struct结构进行操作的方法,有部分API是通过map进行数据库相关操作,那么就需要我们把struct转化成map,下面是是我尝试两种不同struct转换成map的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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形式转换的两倍。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步