Go development Struct conversion to map comparison of two ways
- 2020-06-19 10:28:13
- OfStack
Do Go development recently when exposed to a new framework gorose orm third party, in the process of use, found no similar beego directly to the operation of the structure of struct method, some API database via the map related operations, then we need to convert struct to map, below is I tried two different struct converts map method
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: " Big sand ",
Email: "dashdisnin@126.com",
School: " Guangzhou first 105 Middle school ",
City: "zhongguoguanzhou",
Company: "sndifneinsifnienisn",
Age: 23,
Sex: "F",
Proviece: "jianxi",
Com: " Guangzhou Lamborghini ",
PostTo: " The blue whale XXXXXXXX",
Buys: "shensinfienisnfieni",
Hos: "zhonsndifneisnidnfie",
}
j, _ := json.Marshal(person)
json.Unmarshal(j, &m)
fmt.Println(m)
fmt.Println(time.Now().Sub(t))
}
1. Convert from struct to json, and from json to map
func StructToMapViaJson() {
m := make(map[string]interface{})
t := time.Now()
person := Persion{
Id: 98439,
Name: "zhaondifnei",
Address: " Big sand ",
Email: "dashdisnin@126.com",
School: " Guangzhou first 105 Middle school ",
City: "zhongguoguanzhou",
Company: "sndifneinsifnienisn",
Age: 23,
Sex: "F",
Proviece: "jianxi",
Com: " Guangzhou Lamborghini ",
PostTo: " The blue whale 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: Guangzhou Lamborghini Hos:zhonsndifneisnidnfie Name:zhaondifnei sndifneinsifnienisn :shensinfienisnfieni Age:23 PostTo Address: Big sandy land School: Guangzhou No.105 Middle School City: F Id:98439 Email: dashdisnin@126.com]
duration:250467
2. Generate map by reflection
func StructToMapViaReflect() {
m := make(map[string]interface{})
t := time.Now()
person := Persion{
Id: 98439,
Name: "zhaondifnei",
Address: " Big sand ",
Email: "dashdisnin@126.com",
School: " Guangzhou first 105 Middle school ",
City: "zhongguoguanzhou",
Company: "sndifneinsifnienisn",
Age: 23,
Sex: "F",
Proviece: "jianxi",
Com: " Guangzhou Lamborghini ",
PostTo: " The blue whale 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 :zhaondifnei zhongguoguanzhou :F Proviece:jianxi: Guangzhou Lamborghini Es773en: es7439 School: Guangzhou No. 105 Middle School Age: 98sand ES7en :23 PostTo: Blue whale dashdisnin@126.com Company: sndifneinsifnienisn]
duration:104239
conclusion
The comparison shows that the conversion by reflection is basically twice as large as the conversion by json.