成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

golang struct, map, json之間的相互轉(zhuǎn)換

瀏覽:150日期:2024-05-29 11:29:18
目錄公共代碼區(qū)域一、map, struct 互轉(zhuǎn)1.map 轉(zhuǎn) struct2、struct 轉(zhuǎn) map二、struct, json 互轉(zhuǎn)三、map, json 互轉(zhuǎn)1.map 轉(zhuǎn) json2.json 轉(zhuǎn) map總結(jié)

本文用于記錄我在 golang 學(xué)習(xí)階段遇到的類(lèi)型轉(zhuǎn)換問(wèn)題,針對(duì)的是 json 、map、struct 之間相互轉(zhuǎn)換的問(wèn)題,用到的技術(shù) json 、mapstructure、reflect 三個(gè)類(lèi)庫(kù)

公共代碼區(qū)域

package mainimport ( 'encoding/json' 'fmt' 'testing')type UserInfoVo struct { Id string `json:'id'` UserName string `json:'user_name'` Address []AddressVo `json:'address'`}type AddressVo struct { Address string `json:'address'`}var beforeMap = map[string]interface{}{ 'id':'123', 'user_name': '酒窩豬', 'address': []map[string]interface{}{{'address': 'address01'}, {'address': 'address02'}},}var User UserInfoVofunc init() { User = UserInfoVo{ Id: '01', UserName: '酒窩豬', Address: []AddressVo{ { Address: '湖南', }, { Address: '北京', }, }, }}一、map, struct 互轉(zhuǎn)1.map 轉(zhuǎn) struct

map 轉(zhuǎn) struct 有兩種方式1.是通過(guò)第三方包 github.com/mitchellh/mapstructure2.通過(guò) map 轉(zhuǎn) json,再通過(guò) json 轉(zhuǎn) struct

第三方包 mapstructure

下載依賴,通過(guò)第三方依賴進(jìn)行轉(zhuǎn)換

go get github.com/goinggo/mapstructure

func TestMapToStructByMod(t *testing.T) { var afterStruct =UserInfoVo{} before := time.Now() err := mapstructure.Decode(beforeMap, &afterStruct) if err!=nil{ fmt.Println(err) } fmt.Printf('result:%+v n',time.Since(before)) fmt.Printf('result:%+v n',afterStruct)}

result:61.757µs result:{Id:123 UserName: Address:[{Address:address01} {Address:address02}]} --- PASS: TestMapToStructByMod (0.00s)PASS

通過(guò) JSON 進(jìn)行轉(zhuǎn)換

先將 map 轉(zhuǎn)換成 JSON,再通過(guò) JSON 轉(zhuǎn)換成 struct操作有點(diǎn)繁瑣

func TestMapToStructByJson(t *testing.T) { beforeMap := map[string]interface {}{ 'id':'123', 'user_name':'酒窩豬', 'address':[]map[string]interface{}{{'address': 'address01'}, {'address': 'address02'}}, } var afterStruct =UserInfoVo{} before := time.Now() marshal, err := json.Marshal(beforeMap) if err!=nil{ fmt.Println('marshal:',err) return } err = json.Unmarshal(marshal, &afterStruct) if err!=nil{ fmt.Println('unmarshal:',err) return } fmt.Println(time.Since(before)) fmt.Printf('resutlt: %+v',afterStruct)}

134.299µsresutlt: {Id:123 UserName:酒窩豬 Address:[{Address:address01} {Address:address02}]}--- PASS: TestMapToStructByJson (0.00s)PASS

總結(jié)問(wèn)題:論性能哪個(gè)更佳?

根據(jù)結(jié)果答案使用 JSON 需要時(shí)間是 134.299µs使用 mapstructure 需要時(shí)間是 61.757µs結(jié)果是使用第三方包 mapstructure 性能更好,那么,是因?yàn)槭裁茨??暫且按下不?/p>2、struct 轉(zhuǎn) map

JSON 序列化轉(zhuǎn)換

先將 struct 轉(zhuǎn)換成字節(jié)數(shù)組,再將字節(jié)數(shù)組轉(zhuǎn)換成 map 打印

func TestStructToMapByJson(t *testing.T) { var resultMap interface{} before := time.Now() jsonMarshal, _ := json.Marshal(User) err := json.Unmarshal(jsonMarshal, &resultMap) if err != nil { fmt.Println(err) return } fmt.Println(time.Since(before)) fmt.Printf('%+v',resultMap)}

158.857µsmap[address:[map[address:湖南] map[address:北京]] id:01 user_name:酒窩豬]--- PASS: TestStructToMapByJson (0.00s)PASS

通過(guò)反射轉(zhuǎn)換

通過(guò)反射獲取 User 的類(lèi)型與值

func TestStructToMapByReflect(t *testing.T) { var resultMap = make(map[string]interface{},10) before := time.Now() ty:=reflect.TypeOf(User) v:=reflect.ValueOf(User) for i := 0; i < v.NumField(); i++ { resultMap[strings.ToLower(ty.Field(i).Name)]=v.Field(i).Interface() } fmt.Println(time.Since(before)) fmt.Printf('%+v',resultMap)}

13.965µsmap[address:[{Address:湖南} {Address:北京}] id:01 username:酒窩豬]--- PASS: TestStructToMapByReflect (0.00s)PASS

總結(jié)問(wèn)題:論性能哪個(gè)更佳?

答案是使用反射的效果更快點(diǎn),沒(méi)有那么多繁瑣的轉(zhuǎn)換,記住在 make 中進(jìn)行初始化大小,我試了下,不指定大小與指定大小時(shí)間上有 3~4µs 的區(qū)別網(wǎng)絡(luò)上還有一種方法是使用 structs 包,不過(guò)我看了下,該依賴包已經(jīng)三年沒(méi)更新了

二、struct, json 互轉(zhuǎn)

1. struct 轉(zhuǎn) json

func TestStructToJsonByJson(t *testing.T) { before := time.Now() marshal, _ := json.Marshal(User) fmt.Println(time.Since(before)) fmt.Printf('%s', marshal)}

116.068µs{'id':'01','user_name':'酒窩豬','address':[{'address':'湖南'},{'address':'北京'}]}--- PASS: TestStructToJsonByJson (0.00s)PASS

2.json 轉(zhuǎn) struct

func TestJsonToStructByJson(t *testing.T) { info:=UserInfoVo{} marshal, _ := json.Marshal(User) before := time.Now() json.Unmarshal(marshal,&info) fmt.Println(time.Since(before)) fmt.Printf('%+v',info)}

23.009µs{Id:01 UserName:酒窩豬 Address:[{Address:湖南} {Address:北京}]}--- PASS: TestJsonToStructByJson (0.00s)PASS

三、map, json 互轉(zhuǎn)1.map 轉(zhuǎn) json

func TestMapToJson(t *testing.T) { before := time.Now() marshal, _ := json.Marshal(beforeMap) fmt.Println(time.Since(before)) fmt.Printf('%s', marshal)}

75.133µs{'address':[{'address':'address01'},{'address':'address02'}],'id':'123','user_name':'酒窩豬'}--- PASS: TestMapToJson (0.00s)PASS

2.json 轉(zhuǎn) map

func TestJsonToMap(t *testing.T) { marshal, _ := json.Marshal(beforeMap) resultMap:=make(map[string]interface{},10) before := time.Now() json.Unmarshal(marshal,&resultMap) fmt.Println(time.Since(before)) fmt.Printf('%+v', resultMap)}

28.728µsmap[address:[map[address:address01] map[address:address02]] id:123 user_name:酒窩豬]--- PASS: TestJsonToMap (0.00s)PASS

總結(jié)

三者之間的轉(zhuǎn)換更多的是關(guān)于如果使用 json 內(nèi)庫(kù),只有在 map 轉(zhuǎn) struct 使用了 mapstructure,struct 轉(zhuǎn) map 使用了反射,其他轉(zhuǎn)換,更多的是使用 json 內(nèi)置庫(kù)進(jìn)行轉(zhuǎn)換

到此這篇關(guān)于golang struct, map, json之間的相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)golang struct, map, json 內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: JavaScript
相關(guān)文章: