濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > Golang自定義結(jié)構(gòu)體轉(zhuǎn)map的操作

Golang自定義結(jié)構(gòu)體轉(zhuǎn)map的操作

熱門標(biāo)簽:不封卡外呼系統(tǒng) 重慶慶云企業(yè)400電話到哪申請(qǐng) 地圖標(biāo)注免費(fèi)定制店 湛江crm外呼系統(tǒng)排名 寧波語音外呼系統(tǒng)公司 宿遷便宜外呼系統(tǒng)代理商 仙桃400電話辦理 鄭州智能語音電銷機(jī)器人價(jià)格 上海極信防封電銷卡價(jià)格

在Golang中,如何將一個(gè)結(jié)構(gòu)體轉(zhuǎn)成map? 本文介紹兩種方法。第一種是是使用json包解析解碼編碼。第二種是使用反射,使用反射的效率比較高,代碼在這里。如果覺得代碼有用,可以給我的代碼倉庫一個(gè)star。

假設(shè)有下面的一個(gè)結(jié)構(gòu)體

func newUser() User {
 name := "user"
 MyGithub := GithubPage{
 URL: "https://github.com/liangyaopei",
 Star: 1,
 }
 NoDive := StructNoDive{NoDive: 1}
 dateStr := "2020-07-21 12:00:00"
 date, _ := time.Parse(timeLayout, dateStr)
 profile := Profile{
 Experience: "my experience",
 Date:    date,
 }
 return User{
 Name:   name,
 Github:  MyGithub,
 NoDive:  NoDive,
 MyProfile: profile,
 }
}
 
type User struct {
 Name   string    `map:"name,omitempty"`    // string
 Github  GithubPage  `map:"github,dive,omitempty"` // struct dive
 NoDive  StructNoDive `map:"no_dive,omitempty"`   // no dive struct
 MyProfile Profile   `map:"my_profile,omitempty"` // struct implements its own method
}
 
type GithubPage struct {
 URL string `map:"url"`
 Star int  `map:"star"`
}
 
type StructNoDive struct {
 NoDive int
}
 
type Profile struct {
 Experience string  `map:"experience"`
 Date    time.Time `map:"time"`
}
 
// its own toMap method
func (p Profile) StructToMap() (key string, value interface{}) {
 return "time", p.Date.Format(timeLayout)
}

json包的marshal,unmarshal

先將結(jié)構(gòu)體序列化成[]byte數(shù)組,再從[]byte數(shù)組序列化成結(jié)構(gòu)體。

data, _ := json.Marshal(user)
m := make(map[string]interface{})
json.Unmarshal(data, m)

優(yōu)勢

使用簡單 劣勢

效率比較慢

不能支持一些定制的鍵,也不能支持一些定制的方法,例如將struct的域展開等。

使用反射

本文實(shí)現(xiàn)了使用反射將結(jié)構(gòu)體轉(zhuǎn)成map的方法。通過標(biāo)簽(tag)和反射,將上文示例的newUser()返回的結(jié)果轉(zhuǎn)化成下面的一個(gè)map。

其中包含struct的域的展開,定制化struct的方法。

map[string]interface{}{
 "name":  "user",
 "no_dive": StructNoDive{NoDive: 1},
  // dive struct field
 "url":   "https://github.com/liangyaopei",
 "star":  1,
  // customized method
 "time":  "2020-07-21 12:00:00",
}

實(shí)現(xiàn)思路 源碼解析

1.標(biāo)簽識(shí)別。

使用readTag方法讀取域(field)的標(biāo)簽,如果沒有標(biāo)簽,使用域的名字。然后讀取tag中的選項(xiàng)。目前支持3個(gè)選項(xiàng)

'-':忽略當(dāng)前這個(gè)域

'omitempty' : 當(dāng)這個(gè)域的值為空,忽略這個(gè)域

'dive' : 遞歸地遍歷這個(gè)結(jié)構(gòu)體,將所有字段作為鍵

如果選中了一個(gè)選項(xiàng),就講這個(gè)域?qū)?yīng)的二進(jìn)制位置為1.。

const (
 OptIgnore  = "-"
 OptOmitempty = "omitempty"
 OptDive   = "dive"
)
 
const (
 flagIgnore = 1  iota
 flagOmiEmpty
 flagDive
)
 
func readTag(f reflect.StructField, tag string) (string, int) {
 val, ok := f.Tag.Lookup(tag)
 fieldTag := ""
 flag := 0
 
 // no tag, use field name
 if !ok {
 return f.Name, flag
 }
 opts := strings.Split(val, ",")
 
 fieldTag = opts[0]
 for i := 1; i  len(opts); i++ {
 switch opts[i] {
 case OptIgnore:
  flag |= flagIgnore
 case OptOmitempty:
  flag |= flagOmiEmpty
 case OptDive:
  flag |= flagDive
 }
 }
 return fieldTag, flag
}

2.結(jié)構(gòu)體的域(field)的遍歷。

遍歷結(jié)構(gòu)體的每一個(gè)域(field),判斷field的類型(kind)。如果是string,int等的基本類型,直接取值,并且把標(biāo)簽中的值作為key。

for i := 0; i  t.NumField(); i++ {
    ...
    switch fieldValue.Kind() {
 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
  res[tagVal] = fieldValue.Int()
 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
  res[tagVal] = fieldValue.Uint()
 case reflect.Float32, reflect.Float64:
  res[tagVal] = fieldValue.Float()
 case reflect.String:
  res[tagVal] = fieldValue.String()
 case reflect.Bool:
  res[tagVal] = fieldValue.Bool()
 default:
 }
  }
}

3.內(nèi)嵌結(jié)構(gòu)體的轉(zhuǎn)換

如果是結(jié)構(gòu)體,先檢查有沒有實(shí)現(xiàn)傳入?yún)?shù)的方法,如果實(shí)現(xiàn)了,就調(diào)用這個(gè)方法。如果沒有實(shí)現(xiàn),就遞歸地調(diào)用StructToMap方法,然后根據(jù)是否展開(dive),來把返回結(jié)果寫入res的map。

for i := 0; i  t.NumField(); i++ {
 fieldType := t.Field(i)
 
 // ignore unexported field
 if fieldType.PkgPath != "" {
  continue
 }
 // read tag
 tagVal, flag := readTag(fieldType, tag)
 
 if flagflagIgnore != 0 {
  continue
 }
 
 fieldValue := v.Field(i)
 if flagflagOmiEmpty != 0  fieldValue.IsZero() {
  continue
 }
 
 // ignore nil pointer in field
 if fieldValue.Kind() == reflect.Ptr  fieldValue.IsNil() {
  continue
 }
 if fieldValue.Kind() == reflect.Ptr {
  fieldValue = fieldValue.Elem()
 }
 
 // get kind
 switch fieldValue.Kind() {
 case reflect.Struct:
  _, ok := fieldValue.Type().MethodByName(methodName)
  if ok {
  key, value, err := callFunc(fieldValue, methodName)
  if err != nil {
   return nil, err
  }
  res[key] = value
  continue
  }
  // recursive
  deepRes, deepErr := StructToMap(fieldValue.Interface(), tag, methodName)
  if deepErr != nil {
  return nil, deepErr
  }
  if flagflagDive != 0 {
  for k, v := range deepRes {
   res[k] = v
  }
  } else {
  res[tagVal] = deepRes
  }
 default:
 }
  }
  ...
}
 
// call function
func callFunc(fv reflect.Value, methodName string) (string, interface{}, error) {
 methodRes := fv.MethodByName(methodName).Call([]reflect.Value{})
 if len(methodRes) != methodResNum {
 return "", nil, fmt.Errorf("wrong method %s, should have 2 output: (string,interface{})", methodName)
 }
 if methodRes[0].Kind() != reflect.String {
 return "", nil, fmt.Errorf("wrong method %s, first output should be string", methodName)
 }
 key := methodRes[0].String()
 return key, methodRes[1], nil
}

4.array,slice類型的轉(zhuǎn)換

如果是array,slice類型,類似地,檢查有沒有實(shí)現(xiàn)傳入?yún)?shù)的方法,如果實(shí)現(xiàn)了,就調(diào)用這個(gè)方法。如果沒有實(shí)現(xiàn),將這個(gè)field的tag作為key,域的值作為value。

switch fieldValue.Kind() {
 case reflect.Slice, reflect.Array:
  _, ok := fieldValue.Type().MethodByName(methodName)
  if ok {
  key, value, err := callFunc(fieldValue, methodName)
  if err != nil {
   return nil, err
  }
  res[key] = value
  continue
  }
      res[tagVal] = fieldValue
      ....
}

5.其他類型

對(duì)于其他類型,例如內(nèi)嵌的map,直接將其返回結(jié)果的值。

switch fieldValue.Kind() {
 ...
 case reflect.Map:
  res[tagVal] = fieldValue
 case reflect.Chan:
  res[tagVal] = fieldValue
 case reflect.Interface:
  res[tagVal] = fieldValue.Interface()
 default:
 }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • golang 如何用反射reflect操作結(jié)構(gòu)體
  • golang 實(shí)現(xiàn)兩個(gè)結(jié)構(gòu)體復(fù)制字段
  • golang通過反射設(shè)置結(jié)構(gòu)體變量的值
  • Golang空結(jié)構(gòu)體struct{}用途,你知道嗎
  • golang修改結(jié)構(gòu)體中的切片值方法
  • golang 結(jié)構(gòu)體初始化時(shí)賦值格式介紹
  • 解決golang結(jié)構(gòu)體tag編譯錯(cuò)誤的問題

標(biāo)簽:海南 青海 儋州 物業(yè)服務(wù) 西雙版納 電子產(chǎn)品 安康 遼寧

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Golang自定義結(jié)構(gòu)體轉(zhuǎn)map的操作》,本文關(guān)鍵詞  Golang,自定義,結(jié)構(gòu),體轉(zhuǎn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Golang自定義結(jié)構(gòu)體轉(zhuǎn)map的操作》相關(guān)的同類信息!
  • 本頁收集關(guān)于Golang自定義結(jié)構(gòu)體轉(zhuǎn)map的操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    阿拉善盟| 安泽县| 平定县| 兰西县| 辽源市| 蓝山县| 大宁县| 嘉义市| 黄冈市| 崇明县| 左云县| 慈溪市| 阿合奇县| 苍梧县| 藁城市| 岳西县| 桐梓县| 和田市| 永靖县| 双江| 尚义县| 双城市| 上虞市| 威海市| 修武县| 成都市| 修文县| 汝阳县| 原阳县| 卢湾区| 渭源县| 嘉峪关市| 九寨沟县| 改则县| 册亨县| 讷河市| 饶平县| 大新县| 德保县| 铜川市| 永登县|