golangjsonmapstructjsonmapstructurereflect
公共代码区域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 互转1.map 转 structmapstructgithub.com/mitchellh/mapstructuremapjsonjsonstruct
第三方包 mapstructure下载依赖,通过第三方依赖进行转换
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
通过 JSON 进行转换mapJSONstruct
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
总结问题:
论性能哪个更佳?
JSONmapstructure
2、struct 转 mapJSON 序列化转换先将 struct 转换成字节数组,再将字节数组转换成 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
通过反射转换通过反射获取 User 的类型与值
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
总结问题:论性能哪个更佳?
makestructs
二、struct, json 互转1. struct 转 jsonfunc 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 转 structfunc 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 互转1.map 转 jsonfunc 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 转 mapfunc 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
总结jsonmapstructmapstructurestructmapjson