最近做Go開(kāi)發(fā)的時(shí)候接觸到了一個(gè)新的orm第三方框架gorose,在使用的過(guò)程中,發(fā)現(xiàn)沒(méi)有類似beego進(jìn)行直接對(duì)struct結(jié)構(gòu)進(jìn)行操作的方法,有部分API是通過(guò)map進(jìn)行數(shù)據(jù)庫(kù)相關(guān)操作,那么就需要我們把struct轉(zhuǎn)化成map,下面是是我嘗試兩種不同struct轉(zhuǎn)換成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: "廣州第十五中學(xué)",
City: "zhongguoguanzhou",
Company: "sndifneinsifnienisn",
Age: 23,
Sex: "F",
Proviece: "jianxi",
Com: "廣州蘭博基尼",
PostTo: "藍(lán)鯨XXXXXXXX",
Buys: "shensinfienisnfieni",
Hos: "zhonsndifneisnidnfie",
}
j, _ := json.Marshal(person)
json.Unmarshal(j, m)
fmt.Println(m)
fmt.Println(time.Now().Sub(t))
}
一、通過(guò)struct轉(zhuǎn)json,json轉(zhuǎn)成map
func StructToMapViaJson() {
m := make(map[string]interface{})
t := time.Now()
person := Persion{
Id: 98439,
Name: "zhaondifnei",
Address: "大沙地",
Email: "dashdisnin@126.com",
School: "廣州第十五中學(xué)",
City: "zhongguoguanzhou",
Company: "sndifneinsifnienisn",
Age: 23,
Sex: "F",
Proviece: "jianxi",
Com: "廣州蘭博基尼",
PostTo: "藍(lán)鯨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:藍(lán)鯨XXXXXXXX Address:大沙地 School:廣州第十五中學(xué) City:zhongguoguanzhou Sex:F Id:98439 Email:dashdisnin@126.com]
duration:250467
二、通過(guò)反射形式生成map
func StructToMapViaReflect() {
m := make(map[string]interface{})
t := time.Now()
person := Persion{
Id: 98439,
Name: "zhaondifnei",
Address: "大沙地",
Email: "dashdisnin@126.com",
School: "廣州第十五中學(xué)",
City: "zhongguoguanzhou",
Company: "sndifneinsifnienisn",
Age: 23,
Sex: "F",
Proviece: "jianxi",
Com: "廣州蘭博基尼",
PostTo: "藍(lán)鯨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:廣州第十五中學(xué) Address:大沙地 Age:23 PostTo:藍(lán)鯨XXXXXXXX Hos:zhonsndifneisnidnfie Email:dashdisnin@126.com Company:sndifneinsifnienisn]
duration:104239
結(jié)論
通過(guò)比較可以看出,通過(guò)反射的形式轉(zhuǎn)換基本上是通過(guò)json形式轉(zhuǎn)換的兩倍。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- golang struct 實(shí)現(xiàn) interface的方法
- golang中struct和interface的基礎(chǔ)使用教程
- 淺談Go語(yǔ)言中的結(jié)構(gòu)體struct & 接口Interface & 反射
- 詳解Angular之constructor和ngOnInit差異及適用場(chǎng)景
- 詳解Angular 中 ngOnInit 和 constructor 使用場(chǎng)景
- golang struct擴(kuò)展函數(shù)參數(shù)命名警告解決方法
- 解析Go語(yǔ)言編程中的struct結(jié)構(gòu)
- Golang學(xué)習(xí)筆記(六):struct
- Go語(yǔ)言中struct的匿名屬性特征實(shí)例分析
- Go語(yǔ)言struct類型介紹
- Go語(yǔ)言struct類型詳解