SoFunction
Updated on 2025-03-04

Detailed explanation of the comparison of two ways of converting Struct into map to Go development

Recently, when I was working on Go development, I came across a new orm third-party framework gorose. During the use, I found that there is no method similar to beego to directly operate the struct structure. Some APIs use maps to perform database-related operations, so we need to convert struct into maps. Here are the methods I tried to convert two different structs into maps.

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 := ()
  person := Persion{
    Id:    98439,
    Name:   "zhaondifnei",
    Address: "Big Sand",
    Email:  "dashdisnin@",
    School:  "Guangzhou No. 15 Middle School",
    City:   "zhongguoguanzhou",
    Company: "sndifneinsifnienisn",
    Age:   23,
    Sex:   "F",
    Proviece: "jianxi",
    Com:   "Guangzhou Lamborghini",
    PostTo:  "Blue Whale XXXXXXXXX",
    Buys:   "shensinfienisnfieni",
    Hos:   "zhonsndifneisnidnfie",
  }
  j, _ := (person)
  (j, &m)
  (m)
  (().Sub(t))
}

1. Transform json through struct, convert json to map

func StructToMapViaJson() {
  m := make(map[string]interface{})
  t := ()
  person := Persion{
    Id:    98439,
    Name:   "zhaondifnei",
    Address: "Big Sand",
    Email:  "dashdisnin@",
    School:  "Guangzhou No. 15 Middle School",
    City:   "zhongguoguanzhou",
    Company: "sndifneinsifnienisn",
    Age:   23,
    Sex:   "F",
    Proviece: "jianxi",
    Com:   "Guangzhou Lamborghini",
    PostTo:  "Blue Whale XXXXXXXXX",
    Buys:   "shensinfienisnfieni",
    Hos:   "zhonsndifneisnidnfie",
  }
  j, _ := (person)
  (j, &m)
  (m)
  ("duration:%d", ().Sub(t))
}

output:
map[Proviece:jianxi Com: Guangzhou Lamborghini Hos:zhonsndifneisnidnfie Name:zhaondifnei Company:sndifneinsifnienisn Buys:shensinfienisnfieni Age:23 PostTo:Blue Whale XXXXXXXX Address: Dashadi School: Guangzhou No. 15 Middle School City:zhongguoguanzhou Sex:F Id:98439 Email:dashdisnin@]
duration:250467

2. Generate maps through reflection

func StructToMapViaReflect() {
  m := make(map[string]interface{})
  t := ()
  person := Persion{
    Id:    98439,
    Name:   "zhaondifnei",
    Address: "Big Sand",
    Email:  "dashdisnin@",
    School:  "Guangzhou No. 15 Middle School",
    City:   "zhongguoguanzhou",
    Company: "sndifneinsifnienisn",
    Age:   23,
    Sex:   "F",
    Proviece: "jianxi",
    Com:   "Guangzhou Lamborghini",
    PostTo:  "Blue Whale XXXXXXXXX",
    Buys:   "shensinfienisnfieni",
    Hos:   "zhonsndifneisnidnfie",
  }
  elem := (&person).Elem()
  relType := ()
  for i := 0; i < (); i++ {
    m[(i).Name] = (i).Interface()
  }
  (m)
  ("duration:%d", ().Sub(t))
}

output:
map[Buys:shensinfienisnfieni Name:zhaondifnei City:zhongguoguanzhou Sex:F Proviece:jianxi Com: Guangzhou Lamborghini Id:98439 School: Guangzhou No. 15 Middle School Address: Dashadi Age:23 PostTo: Blue Whale XXXXXXXX Hos:zhonsndifneisnidnfie Email:dashdisnin@ Company:sndifneinsifnienisn]
duration:104239

in conclusion

From comparison, it can be seen that the form conversion through reflection is basically twice as much as that through json form.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.