欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年12月19日 11:25:11   作者:liangyaopei_  
這篇文章主要介紹了Golang自定義結(jié)構(gòu)體轉(zhuǎn)map的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

假設有下面的一個結(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的域展開等。

使用反射

本文實現(xiàn)了使用反射將結(jié)構(gòu)體轉(zhuǎn)成map的方法。通過標簽(tag)和反射,將上文示例的newUser()返回的結(jié)果轉(zhuǎn)化成下面的一個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",
}

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

1.標簽識別。

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

'-':忽略當前這個域

'omitempty' : 當這個域的值為空,忽略這個域

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

如果選中了一個選項,就講這個域?qū)亩M制位置為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)體的每一個域(field),判斷field的類型(kind)。如果是string,int等的基本類型,直接取值,并且把標簽中的值作為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)體,先檢查有沒有實現(xiàn)傳入?yún)?shù)的方法,如果實現(xiàn)了,就調(diào)用這個方法。如果沒有實現(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 flag&flagIgnore != 0 {
  continue
 }
 
 fieldValue := v.Field(i)
 if flag&flagOmiEmpty != 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 flag&flagDive != 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類型,類似地,檢查有沒有實現(xiàn)傳入?yún)?shù)的方法,如果實現(xiàn)了,就調(diào)用這個方法。如果沒有實現(xiàn),將這個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.其他類型

對于其他類型,例如內(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:
 }

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

相關文章

  • go語言中for?range使用方法及避坑指南

    go語言中for?range使用方法及避坑指南

    Go中的for range組合可以和方便的實現(xiàn)對一個數(shù)組或切片進行遍歷,但是在某些情況下使用for range時很可能就會被"坑",下面這篇文章主要給大家介紹了關于go語言中for?range使用方法及避坑指南的相關資料,需要的朋友可以參考下
    2022-09-09
  • 基于Go語言實現(xiàn)簡單的計算器

    基于Go語言實現(xiàn)簡單的計算器

    這篇文章主要為大家詳細介紹了如何基于Go語言實現(xiàn)簡單的計算器,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-10-10
  • go語言學習之包和變量詳解

    go語言學習之包和變量詳解

    這篇文章主要給大家愛介紹了關于go語言學習之包和變量的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用go語言具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-06-06
  • golang數(shù)組和切片作為參數(shù)和返回值的實現(xiàn)

    golang數(shù)組和切片作為參數(shù)和返回值的實現(xiàn)

    本文主要介紹了golang數(shù)組和切片作為參數(shù)和返回值的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Go語言學習之鏈表的使用詳解

    Go語言學習之鏈表的使用詳解

    鏈表是一種物理存儲單元上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。本文將詳細為大家介紹Go語言中鏈表的使用,感興趣的可以了解一下
    2022-04-04
  • 詳解如何在Golang中監(jiān)聽多個channel

    詳解如何在Golang中監(jiān)聽多個channel

    這篇文章主要為大家詳細介紹了如何在Golang中實現(xiàn)監(jiān)聽多個channel,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-03-03
  • 解決vscode中g(shù)olang插件依賴安裝失敗問題

    解決vscode中g(shù)olang插件依賴安裝失敗問題

    這篇文章主要介紹了解決vscode中g(shù)olang插件依賴安裝失敗問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • 詳解Go內(nèi)存模型

    詳解Go內(nèi)存模型

    這篇文章主要介紹了Go 內(nèi)存模型的相關資料,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • go語言 bool類型的使用操作

    go語言 bool類型的使用操作

    這篇文章主要介紹了go語言 bool類型的使用操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • golang執(zhí)行命令獲取執(zhí)行結(jié)果狀態(tài)(推薦)

    golang執(zhí)行命令獲取執(zhí)行結(jié)果狀態(tài)(推薦)

    這篇文章主要介紹了golang執(zhí)行命令獲取執(zhí)行結(jié)果狀態(tài)的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2019-11-11

最新評論